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

  • Mastering Visual Studio 2017
  • Kunal Chowdhury
  • 124字
  • 2021-07-15 17:26:39

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:

主站蜘蛛池模板: 中牟县| 贡山| 徐州市| 天津市| 铜梁县| 兰坪| 濮阳县| 重庆市| 桂阳县| 崇左市| 巴南区| 镇赉县| 常熟市| 屯门区| 奇台县| 右玉县| 迁西县| 六安市| 黔东| 鄂托克旗| 丽江市| 遂昌县| 乃东县| 岢岚县| 凭祥市| 奇台县| 新沂市| 扎鲁特旗| 青州市| 晋宁县| 靖安县| 如东县| 揭阳市| 莲花县| 临泉县| 万盛区| 溆浦县| 陵水| 冷水江市| 平阴县| 佳木斯市|