官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 河池市| 韩城市| 江门市| 吉木乃县| 盐山县| 嵊泗县| 武宣县| 灌阳县| 华蓥市| 西畴县| 江阴市| 喀什市| 同仁县| 万山特区| 洪湖市| 江山市| 孟村| 榆中县| 涪陵区| 蚌埠市| 甘德县| 乐陵市| 西充县| 辉南县| 尼木县| 海口市| 宝鸡市| 南靖县| 云霄县| 青田县| 赤壁市| 卓尼县| 灯塔市| 遂川县| 子洲县| 大方县| 景谷| 闸北区| 金寨县| 南陵县| 惠来县|