C# readonly const
readonly关键字是可以在字段上使用的修饰符。当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中。
在此示例中,字段 year 的值无法在 ChangeYear 方法中更改,即使在类构造函数中给它赋了值。
1 class Age 2 { 3 readonly int _year; 4 Age(int year) 5 { 6 _year = year; 7 } 8 void ChangeYear() 9 { 10 //_year = 1967; // Compile error if uncommented. 11 } 12 }
- 只能当在声明中初始化变量时,例如:
public readonly int y = 5;
- 或对于实例字段,在包含字段声明的类的实例构造函数中;或对于静态字段,在包含字段声明的类的静态构造函数中。 也只有在这些上下文中,将 readonly 字段作为 out 或 ref 参数传递才有效。
1 public class ReadOnlyTest 2 { 3 class SampleClass 4 { 5 public int x; 6 // Initialize a readonly field 7 public readonly int y = 25; 8 public readonly int z; 9 10 public SampleClass() 11 { 12 // Initialize a readonly instance field 13 z = 24; 14 } 15 16 public SampleClass(int p1, int p2, int p3) 17 { 18 x = p1; 19 y = p2; 20 z = p3; 21 } 22 } 23 24 static void Main() 25 { 26 SampleClass p1 = new SampleClass(11, 21, 32); // OK 27 Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z); 28 SampleClass p2 = new SampleClass(); 29 p2.x = 55; // OK 30 Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z); 31 } 32 } 33 /* 34 Output: 35 p1: x=11, y=21, z=32 36 p2: x=55, y=25, z=24 37 */
在前面的示例中,如果使用这样的语句:
p2.y = 66; // Error
将收到编译器错误信息:
The left-hand side of an assignment must be an l-value
这与尝试将值赋给常数时收到的错误相同。
readonly 关键字与 const 关键字不同。const 字段只能在该字段的声明中初始化。readonly 字段可以在声明或构造函数中初始化。因此,根据所使用的构造函数,readonly 字段可能具有不同的值。另外,const 字段为编译时常数,而 readonly 字段可用于运行时常数,如下例所示:
public static readonly uint timeStamp = (uint)DateTime.Now.Ticks;
Reference: http://msdn.microsoft.com/zh-cn/library/acdd6hb7.aspx
Additional:
The modifier readonly means that the value cannot be assigned except in the declaration or constructor. It does not mean that the assigned object becomes immutable(不可变)。
1 public static class MyList 2 { 3 public static readonly SortedList<int, List<myObj>> CharList; 4 // ...etc. 5 }
But even using readonly, you can still add items to the list from another class.
MyList.CharList[100] = new List<myObj>() { new myObj(30, 30) }; //or MyList.CharList.Add(new List<myObj>() { new myObj(30, 30) });
转载于:https://www.cnblogs.com/wxin/p/3596385.html
C# readonly const相关推荐
- C#中readonly, const 和 in型参数使用
readonly, const 和 in, 都是C# 关键字 它们不能被用于函数参数 ref/out 型参数总是被赋于写的权力 转载于:https://blog.51c ...
- const与readonly,析构函数,虚拟方法
const 与readonly const 关键字用于修改字段或局部变量的声明.它指定字段或局部变量的值不能被修改.常数声明引入给定类型的一个或多个常数. 常数声明可以声明多个常数,例如 ...
- 二道Const,readonly 和 override, new的面试题
1. Const 和 readonly const int bb = aa * 10;const int aa = 10;static readonly int cc = dd * 10;static ...
- C#中const和readonly有什么区别?
C#中const和readonly什么区别? 您什么时候可以使用另一个? #1楼 常量成员是在编译时定义的,不能在运行时更改. 使用const关键字将const声明为字段,并且必须在声明const对其 ...
- let const 命令
一.let命令 用于声明变量. 1) 所声明的变量只在let命令所在代码块内有效.(块级作用域) {let a=10;var b=1; } a // ReferenceError: a is not ...
- 除了允许变量被const函数修改之外,'mutable'关键字是否有任何其他用途?
前段时间我遇到了一些用<code>mutable关键字标记类的成员变量的代码. 据我所知,它只是允许你修改const方法中的变量: class Foo { private: mutable ...
- C# swich, constant, readonly,static, StringBuilder 和字段与属性
switch语句 switch语句中各个常量表达式的值不能相等. 此外, C#还规定每个case分支都必须以break.return.goto或throw语句来结束, 而且语句中的任何代码都不能修改控 ...
- (5)风色从零单排《C++ Primer》 const,typedef,auto,decltype
从零单排<C++ Primer> --(5)const,typedef,auto,decltype CONST 多文件下 //file_1.cc defines and initial ...
- 使用js控制readonly属性
1.使用原生JS <!DOCTYPE html> <html lang="en"> <head><meta charset="U ...
最新文章
- ASP .NET Core Web开发之 Tag Helpers
- ArrayList初步
- Springbooot集成Shiro简单使用
- ArcGIS教程:Iso 聚类非监督分类
- 动手学CV-目标检测入门教程6:训练与测试
- 南开调整研究生奖助,博士生最高超10万,硕士生6万,可能已经超过他们毕业后能拿的工资!...
- centeros7安装mysql5.6_CentOS7安装MySQL5.6
- 2021高考分数文科成绩查询,2021高考分数线预测 文科理科分数线是多少
- Centos7 密码重置
- python(45)内置函数:os.system() 和 os.popen()
- java中ssm付款代码_基于Java+SSM的网上订餐系统、基于JavaWeb的网上订餐系统
- JAVA实现简单计算器布局与功能(附完整源码)
- 收费系统java源代码_java网吧计费系统源代码
- 解决 Kotlin 换页符提示错误 Illegal escape f 无法使用问题
- 人工智能实验1-波士顿房价预测
- 移位寄存器SHIFT RAM IP之模拟图像卷积
- 如何用java制作动画_如何制作动画(java实现动画的代码)
- 报告解读下载 | 12月《中国数据库行业分析报告》发布,精彩抢先看
- Java自学资料!靠着这份面试题跟答案
- IDEA 的Surround With 快捷键只显示live template问题