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

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:

主站蜘蛛池模板: 秦皇岛市| 疏附县| 股票| 凉山| 泸西县| 桐庐县| 沁水县| 正安县| 乡城县| 凌源市| 长沙市| 大名县| 长乐市| 正宁县| 吉林省| 武鸣县| 竹北市| 寻乌县| 京山县| 和田县| 原平市| 明溪县| 成都市| 登封市| 纳雍县| 乐业县| 彭泽县| 长寿区| 孟村| 奎屯市| 同德县| 宜州市| 肥西县| 图们市| 时尚| 大厂| 荥经县| 监利县| 宁河县| 龙陵县| 建宁县|