Changes to the expression-bodied members

In C# 6.0, Microsoft introduced the expression-bodied methods and properties, but these had a few limitations, which didn't allow us to use them in the constructors, destructors, and getters/setters of properties.

With C# 7.0, these limitations are no more, and you can now write them for single-liner constructors and destructors, as well as the getter and setter of a property. Here's how you can use them:

    public class Person 
    { 
      private string m_name; 
 
      // constructor 
      public Person() => Console.WriteLine("Constructor called"); 
 
      // destructor 
      ~Person() => Console.WriteLine("Destructor called"); 
 
      // getter/setter properties 
      public string Name 
      { 
        get => m_name; 
        set => m_name = value; 
      } 
    } 

When you run the preceding code, the following output can be seen in the console window:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.70.203