国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Usage of readonly and const

2019-11-17 03:05:31
字體:
來源:轉載
供稿:網友

Usage of readonly and const

Many new learners can not make sure the usage scenarios of readonly and const keyWords. In my opinion, the main reason of using readonly keyword is the efficiency. but in most occasions, efficiency isn’t treated as the high level. so I’m willing to use readonly keyword. because of it’s much more flexibility. The nature differences of these two keywords are as below:

one: const is a compile-time constant, but readonly is a runtime constant.

two: const can only modify the PRimitive types, enum types and string types, but readonly has no limits.

For the first point, because of the compile-time constant feature, so natively it’s static and you can’t add the static modifier manually.

eg: static const int value = 100;

For the efficiency of const keyword, after compiled by the compiler, the constants where they are used will be replaced by it’s real value.

For the readonly variable , it’s assigned when in runtime, then it’s value can not be changed.

The so-called can not be changed contains two meanings.

  1. For value type variable, it’s value can not be changed.
  2. For reference type variable, it’s reference can not be changed, but the value of it’s referenced instance can be changed.

eg:

class Istone{  public readonly int age;  public Istone(int age)  {        this.age = age;   }}Istone istone = new Istone(20);istone.age = 300;         //it’s wrong, can’t assign value again for readonly variable

  

For the reference variable, after giving an example, you will rapidly understand.

eg:

 class  Istone2{  public readonly Student stu;  public Istone2(Student stu)  {     this.stu = stu;  }}Istone2 stu2 = new Istone2(new Student(){age= 10});stu2.stu = new Student(){age = 20};//wrong

  

Can not assign value to readonly field, but can change it’s referenced instance like below:

stu2.stu.age = 20;

PS: readonly variable can be initialized, and then assigned value one or more time in the constructor.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漳平市| 麟游县| 若羌县| 灵寿县| 和林格尔县| 黄龙县| 大方县| 尤溪县| 崇文区| 锡林浩特市| 彝良县| 桐庐县| 兰西县| 九龙坡区| 澄江县| 洛阳市| 嘉鱼县| 阿拉尔市| 蛟河市| 当涂县| 南城县| 北安市| 嵊泗县| 开平市| 安远县| 萨迦县| 抚州市| 墨竹工卡县| 丹凤县| 炉霍县| 太和县| 闽侯县| 恩施市| 五华县| 泾川县| 怀仁县| 盘山县| 渭源县| 东宁县| 凤冈县| 许昌县|