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. Java的观察者

    class Teacher extends Observable { public void startLesson() { System.out.println(String.format(&quo ...

  2. Nginx常用rewrite跳转重定向实例

    1.将www.myweb.com/connect 跳转到connect.myweb.com rewrite ^/connect$ http://connect.myweb.com permanent; ...

  3. IDEA中Java代码存入DB中为乱码

    有一种可能是编译后出现的乱码,可以在Setting的Java compiler中加如下 -encoding UTF-8

  4. eclipse+pydev 安装和配置过程

    安装 PyDev 在安装 PyDev 之前,要保证您已经安装了 Java 1.4 或更高版本.Eclipse 以及 Python.接下来,开始安装 PyDev 插件. 启动 Eclipse,利用 Ec ...

  5. 五种I/O模型的学习

    来自   http://www.52im.net/thread-1935-1-1.html 4.互联网服务端处理网络请求的原理 首先看看一个典型互联网服务端处理网络请求的典型过程:<ignore ...

  6. Java 读写Properties配置文件【转】

    1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...

  7. cad 安装失败/出错/卸载 2018/2017/2016/2015/2013/2012

    AUTO Uninstaller 更新下载地址 1.选择CAD 2.选择版本 3.点击“开始卸载”

  8. MATLAB矩阵操作和算术运算符

    矩阵的表示 矩阵之间用空格或者是逗号间隔 矩阵可以拼接(可以用矩阵拼接) 实部矩阵和虚部矩阵构成复数矩阵,一一对应. 冒号表达式: 格式: e1:e2:e3 e1表示初始值    e2表示步长   e ...

  9. 60、Docker 学习笔记(CentOS 7.1)

    #基本概念 -x86_64-minimal.tar.gz | docker import - centos:v7.mini``` 然后查看导入的镜像: ##上传镜像 >用户可以通过 docker ...

  10. 学习JVM-GC原理

    1. 前言 Java和C++之间显著的一个区别就是对内存的管理.和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称G ...