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

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:

主站蜘蛛池模板: 龙口市| 大洼县| 神池县| 奇台县| 奉化市| 商丘市| 新营市| 句容市| 从江县| 连山| 西城区| 姜堰市| 勃利县| 法库县| 锡林浩特市| 嘉善县| 七台河市| 石家庄市| 兴海县| 道真| 定日县| 象州县| 阜南县| 曲周县| 宣恩县| 仙桃市| 九龙城区| 凭祥市| 阿勒泰市| 道孚县| 高安市| 双峰县| 黄龙县| 包头市| 东兴市| 宝坻区| 土默特左旗| 大田县| 德昌县| 石渠县| 铜川市|