- 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:

推薦閱讀
- Mastering JavaScript Functional Programming
- Java程序員面試算法寶典
- NumPy Essentials
- Hands-On C++ Game Animation Programming
- 精通網絡視頻核心開發技術
- 小學生C++創意編程(視頻教學版)
- 好好學Java:從零基礎到項目實戰
- Kotlin開發教程(全2冊)
- QGIS Python Programming Cookbook(Second Edition)
- Buildbox 2.x Game Development
- Python編程:從入門到實踐(第3版)
- 大學計算機基礎實訓教程
- C語言程序設計
- Node.js實戰:分布式系統中的后端服務開發
- 高質量程序設計指南:C++/C語言