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. TCP的粘包问题

    什么是粘包 粘包指的是数据与数据之间没有明确的分界线,导致不能正确读取 应用程序无法直接操作硬件,应用程序想要发送数据则必须将数据交给操作系统,而操作系统需要同时为所有应用程序提供数据传输服务,也就意 ...

  2. VS Ctrl + Shift + Q

    在VS中按 Ctrl + Shift + Q 可以快速查找 void OnCollisionStay  等方法. void OnCollisionStay(Collision collision) { ...

  3. [转]js add month 加n月

    本文转自:http://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript/5645126 I ...

  4. php表单提交时的身份证号码验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. springmvc+mybatis+sql server实现简单登录功能

    一.源码: 1.Users.java package com.login.entity; import java.io.Serializable; public class Users impleme ...

  6. Linux大文件快速处理小方法

    背景 工作中使用MapReduce任务导出一批含有路径的文件,共计行数300W+,需要检测文件是否在对应的服务器中存在,而文件所在的服务器并非hadoop集群的服务器,因此打算采用bash脚本进行.具 ...

  7. css悬浮右侧悬浮

    <html><head><title>CSSDemo</title><style type"text/css"> .ho ...

  8. 拼json对象批量向后台添加数据

    网站中如果遇到批量提交格式相同数据,可以使用json来传输 $("#catalogSave").click(function(){ var array=[]; $("[n ...

  9. [JAVA][Liferay] Configure sharding in multiple sites

    create databases first portal-ext.properties配置 hibernate.dialect=org.hibernate.dialect.PostgreSQLDia ...

  10. css 引入方式以及css的选择器

    一.css的引入方式: 1.行内样式 <div> <p style="color: red">我是一个段落</p> </div> 2 ...