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

Out variables

With C# 7.0, we can write cleaner code when using out variables. As we know, to use out variables, we have to first declare them. With the new language enhancement, we can now just write out as a prefix and specify the name of the variable that we need that value to be assigned to.

To clarify this concept, we will first see the traditional approach, which is shown as follows:

public void GetPerson() 
{ 
  int year; 
  int month; 
  int day; 
  GetPersonDOB(out year, out month, out day); 
} 
 
public void GetPersonDOB(out int year, out int month, out int day ) 
{ 
  year = 1980; 
  month = 11; 
  day = 3; 
} 

And here with C# 7.0, we can simplify the preceding GetPerson method, which is shown as follows:

public void GetPerson() 
{ 
  GetPersonDOB(out int year, out int month, out int day); 
} 
主站蜘蛛池模板: 阿拉善右旗| 温宿县| 新密市| 屏东县| 嘉义县| 南涧| 伊春市| 余姚市| 新宾| 天峻县| 灵川县| 双桥区| 扬中市| 霸州市| 青浦区| 特克斯县| 化隆| 平罗县| 鸡西市| 神农架林区| 安义县| 紫云| 彭水| 滁州市| 普洱| 孙吴县| 宁乡县| 南川市| 平远县| 花莲县| 阜康市| 江西省| 进贤县| 宝清县| 大邑县| 镇原县| 芷江| 南郑县| 永平县| 梁平县| 武山县|