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

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.

主站蜘蛛池模板: 蒲江县| 元阳县| 鄱阳县| 磴口县| 静宁县| 阿城市| 临沂市| 永州市| 邵东县| 海阳市| 海伦市| 南陵县| 东乌珠穆沁旗| 玛沁县| 两当县| 宝坻区| 怀来县| 双鸭山市| 霍林郭勒市| 孝义市| 洪洞县| 奇台县| 阜康市| 灵山县| 阜南县| 桂阳县| 饶河县| 恭城| 江城| 永吉县| 高唐县| 图木舒克市| 舞阳县| 内江市| 景洪市| 聊城市| 兴义市| 漠河县| 上杭县| 泰和县| 仁化县|