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

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:

主站蜘蛛池模板: 淮安市| 禹城市| 全州县| 五指山市| 六盘水市| 灵丘县| 静乐县| 康保县| 江口县| 池州市| 会宁县| 普洱| 类乌齐县| 抚顺县| 南皮县| 桓仁| 门头沟区| 河津市| 鄯善县| 宜丰县| 通榆县| 大方县| 定州市| 玛多县| 洛宁县| 平凉市| 仙桃市| 东乌| 诸暨市| 修文县| 萝北县| 武威市| 岳西县| 修文县| 德江县| 积石山| 茶陵县| 连云港市| 新乐市| 盘锦市| 胶州市|