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

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.

主站蜘蛛池模板: 南丹县| 盐山县| 龙山县| 湘乡市| 博客| 南和县| 五常市| 怀化市| 凤城市| 麻栗坡县| 谢通门县| 奉新县| 平泉县| 鲁甸县| 徐水县| 淮南市| 墨竹工卡县| 长寿区| 新和县| 麦盖提县| 三江| 龙里县| 桓台县| 奉贤区| 玉门市| 黑山县| 宜阳县| 土默特左旗| 台州市| 平利县| 平顺县| 滨海县| 武强县| 石阡县| 疏勒县| 永吉县| 正蓝旗| 丽水市| 井冈山市| 广东省| 临安市|