- C# 7 and .NET Core 2.0 High Performance
- Ovais Mehboob Ahmed Khan
- 177字
- 2021-08-27 18:47:10
Expression bodied member extended
Expression bodied members were introduced in C# 6.0 where the syntactical expression of the method can be written in a simpler way. In C# 7.0, we can use this feature with a constructor, a destructor, an exception, and so on.
The following example shows how the constructor and destructor syntactic expressions can be simplified using expression bodied members:
public class PersonManager { //Member Variable Person _person; //Constructor PersonManager(Person person) => _person = person; //Destructor ~PersonManager() => _person = null; }
With properties, we can also simplify the syntactic expression, and the following is a basic example of how this can be written:
private String _name; public String Name { get => _name; set => _name = value; }
We can also use an expression bodied syntactic expression with exceptions and simplify the expression, which is shown as follows:
private String _name; public String Name { get => _name; set => _name = value ?? throw new ArgumentNullException(); }
In the preceding example, if the value is null, a new ArgumentNullException will be thrown.
推薦閱讀
- 數據浪潮
- 計算機綜合設計實驗指導
- 數據挖掘原理與實踐
- Test-Driven Development with Mockito
- iOS and OS X Network Programming Cookbook
- Creating Dynamic UIs with Android Fragments(Second Edition)
- 城市計算
- 數據庫原理與應用(Oracle版)
- 數據庫技術及應用教程
- ZeroMQ
- SQL Server 2012數據庫管理教程
- Hadoop大數據開發案例教程與項目實戰(在線實驗+在線自測)
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- 計算機視覺
- Mastering ROS for Robotics Programming(Second Edition)