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. 经典SQL行列转换

    -- http://www.programbbs.com/doc/4885.htm /* 标题:普通行列转换(version 2.0) 说明:普通行列转换(version 1.0)仅针对sql ser ...

  2. environment与@ConfigurationProperties的关系 加载过程分析

    environment是在printBanner之前就初始化好了, 更在context创建之前, 已经加载application-xxxx.properties, System.properties, ...

  3. servlet3

    亿级流量架构 http://www.iteye.com/blogs/subjects/as-core servlet3.1对比 http://jinnianshilongnian.iteye.com/ ...

  4. my09_mysql指定时间点恢复之binlog start-position

    场景描述 ********************************************* 在远程服务器做的全备并已经恢复,同时使用binlog server备份binlog 之后,删除了库 ...

  5. 关于box-shadow、border-radius不兼容ie8的解决办法

    本来从css3兼容ie9+挺好的,可是总有一些共识要求ie8+,于是就有了我们的苦逼的找解决办法.之前在网上查到一些说用 PIE.htc. But 我就是按照他说的写的没有管用.请教了一下别人才会写了 ...

  6. Java基础07-随机数

    第一种方法:导入java.util.Random; import java.util.Random; public class Test1{ public static void main(Strin ...

  7. Sphinx Building Docs in horizon

    Building Contributor Documentation This documentation is written by contributors, for contributors. ...

  8. eclipse中使用git下载项目

    准备工作: 目的:从远程仓库github上down所需的项目 eclipse使用git插件下载github上项目 eclipse版本:eclipse4.5  64位 jdk版本:jdk-1.7 64位 ...

  9. 《Flink 源码解析》—— 源码编译运行

    更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...

  10. 转:MVC中的文件上传

    上传文件与与上传数据区别 上传数据主要指json等简单字符串,上传文件指的是上传word.excel图片等.在上传数据的时候enctype默认为第一个application/x-www-form-ur ...