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

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:

主站蜘蛛池模板: 西林县| 浠水县| 海晏县| 汾西县| 云梦县| 鹤岗市| 页游| 平武县| 会东县| 左权县| 彩票| 大名县| 洞头县| 通榆县| 庆阳市| 临澧县| 铁岭市| 阿瓦提县| 许昌县| 乌拉特前旗| 讷河市| 化隆| 庐江县| 兴山县| 苗栗市| 义乌市| 马山县| 遵义市| 龙胜| 寻乌县| 新丰县| 建阳市| 鹤壁市| 涟源市| 石泉县| 宝鸡市| 斗六市| 洛隆县| 龙山县| 田东县| 宝坻区|