You can make a field in a class read-only by using the readonly modifier when the field is declared.

In the example below, code that creates or uses instances of the Dog class will be able to read the SerialNumber field, but not write to it.

 public class Dog
{
public string Name;
public int Age;
public readonly string SerialNumber; public Dog(string name, int age, string serNumber)
{
Name = name;
Age = age;
SerialNumber = serNumber;
}
}

You can assign a value to a readonly field in only two different places:

  • In the class constructor
  • As part of the field's declartion
 public readonly string BestFriend = "Man";

原文地址:#336 - Declaring and Using a readonly Field

【转载】#336 - Declaring and Using a readonly Field的更多相关文章

  1. react.js Warning: Failed form propType: You provided a value prop to a form field without an onChange handler. This will render a read-only field.

    错误信息: eact.js:20483 Warning: Failed form propType: You provided a value prop to a form field without ...

  2. ReadOnly field saved with NULL value

    On CRM opportunity form view, i added readonly="1" for probability field. When i saved, wh ...

  3. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  4. Just4Fun - Comparaison between const and readonly in C#

    /* By Dylan SUN */ Today let us talk about const and readonly. const is considered as compile-time c ...

  5. readonly

    readonly 关键字是可以在字段上使用的修饰符.  当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中. 示例     在此示例 ...

  6. static, readonly, const

    static Use the static modifier to declare a static member, which belongs to the type itself rather t ...

  7. const, static and readonly

    const, static and readonly http://tutorials.csharp-online.net/const,_static_and_readonly Within a cl ...

  8. A const field of a reference type other than string can only be initialized with null Error [duplicate]

    I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...

  9. Usage of readonly and const

    Many new learners can not make sure the usage scenarios of readonly and const keywords. In my opinio ...

随机推荐

  1. Git for Linux and Windows

    1.在liunx中安装 1.1yum安装 [root@node1 ~]# yum install git –y [root@node1 ~]# git version git version 1.8. ...

  2. 通过JS,按照原比例控制图片尺寸

    <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...

  3. Oozie的缺点

    Oozie使用的时候有以下不便: [a]Oozie调度的Workflow只能使用XML文件配置 [b]启动调度只能通过命令行 [c]无法通过Oozie界面调试调度脚本 [d]Oozie无法可视化调试脚 ...

  4. 关于element-ui表格样式设置的方法cell-class-name

    关于element-ui表格使用的一些方法 最近在用Vue.js和elment-ui做一个后台管理项目,不得不说element功能非常强大,提供了许多组件,基本可以满足一些基础的开发了.因为我做的后台 ...

  5. Error in event handler for "el.form.change": "TypeError: value.getTime is not a function"

    首先说一下我使用的实际场景 html代码: js代码: 首先说明出现原因,elementUI的日期选择器[el-date-picker]在加上格式 value-format="yyyy-MM ...

  6. Python函数调用

    1.同级目录下的调用 - src |- mod1.py |- test1.py 若在程序test1.py中导入模块mod1.py,则直接使用 import mod1 或 from mod1 impor ...

  7. 2019.03.20 读书笔记 as is 以及重写隐式/显示

    强转.as is 的用法 强制转换类型有两种:子类转基类,重写隐式(implicit )\显示(explicit) 转换操作符 class myclass { private int value; p ...

  8. jsp中判断if 和 else

    <c:choose>                   <c:when test="${date==datetime}">                ...

  9. (转)DB2 8.2 for aix5L安装和配置步骤

    DB2 8.2 for aix5L安装和配置步骤[@more@] AIX5.2上安装DB2 V8.2安装平台:IBM eserver,AIX 5.3 64位 一 删除以有的DB2系统: 1.停止DB2 ...

  10. DedeCMS {dede:field name='array' /}的用法

    field支持用array获取任意字段的值.(支持标记:文章列表模板的 {dede:field name='array' /}.arclist.arclistsg.loop.sql 标签)   例如平 ...