【转】const和static readonly

我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等。在多数情况下可以混用。
二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值。
明白了这个本质区别,我们就不难看出下面的语句中static readonly和const能否互换了:

1. static readonly MyClass myins = new MyClass();
2. static readonly MyClass myins = null;
3. static readonly A = B * 20;
   static readonly B = 10;
4. static readonly int [] constIntArray = new int[] {1, 2, 3};
5. void SomeFunction()
   {
      const int a = 10;
      ...
   }

1:不可以换成const。new操作符是需要执行构造函数的,所以无法在编译期间确定
2:可以换成const。我们也看到,Reference类型的常量(除了String)只能是Null。
3:可以换成const。我们可以在编译期间很明确的说,A等于200。
4:不可以换成const。道理和1是一样的,虽然看起来1,2,3的数组的确就是一个常量。
5:不可以换成readonly,readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员。

因此,对于那些本质上应该是常量,但是却无法使用const来声明的地方,可以使用static readonly。

例如C#规范中给出的例子:

 1 public class Color
 2 {
 3     public static readonly Color Black = new Color(0, 0, 0);
 4     public static readonly Color White = new Color(255, 255, 255);
 5     public static readonly Color Red = new Color(255, 0, 0);
 6     public static readonly Color Green = new Color(0, 255, 0);
 7     public static readonly Color Blue = new Color(0, 0, 255);
 8
 9     private byte red, green, blue;
10
11     public Color(byte r, byte g, byte b)
12     {
13         red = r;
14         green = g;
15         blue = b;
16     }
17 }

static readonly需要注意的一个问题是,对于一个static readonly的Reference类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。 
public static readonly MyClass myins = new MyClass();

myins.SomeProperty = 10;  //正常
myins = new MyClass();    //出错,该对象是只读的

但是,如果上例中的MyClass不是一个class而是一个struct,那么后面的两个语句就都会出错。

Const 和 static readonly的区别:

可能通过上述纯概念性的讲解,对有些初学者有些晕乎。下面就一些例子来说明下:

 1 using System;
 2 class P
 3 {
 4     static readonly int A=B*10;
 5     static readonly int B=10;
 6     public static void Main(string[] args)
 7     {
 8         Console.WriteLine("A is {0},B is {1} ",A,B);
 9     }
10 }

对于上述代码,输出结果是多少?很多人会认为是A is 100,B is 10吧!其实,正确的输出结果是A is 0,B is 10。

好吧,如果改成下面的话:using System;

 1 class P
 2 {
 3     const int A=B*10;
 4     const int B=10;
 5     public static void Main(string[] args)
 6     {
 7         Console.WriteLine("A is {0},B is {1} ",A,B);
 8     }
 9
10 }

对于上述代码,输出结果又是多少呢?难道是A is 0,B is 10?其实又错了,这次正确的输出结果是A is 100,B is

那么为什么是这样的呢?其实在上面说了,const是静态常量,所以在编译的时候就将A与B的值确定下来了(即B变量时10,而A=B*10=10*10=100),那么Main函数中的输出当然是A is 100,B is 10啦。而static readonly则是动态常量,变量的值在编译期间不予以解析,所以开始都是默认值,像A与B都是int类型,故都是0。而在程序执行到A=B*10;所以A=0*10=0,程序接着执行到B=10这句时候,才会真正的B的初值10赋给B。

时间: 2024-08-07 20:51:59

【转】const和static readonly的相关文章

[转]C# const和static readonly区别

我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值.而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值.明白了这个本质区别,我们就不难看出下面的语句中static readonly和const能否互换了: 1. static readonly MyClass myins = new M

(C#) What is the difference between "const" and "static readonly" ?

const int a must be initialized initialization must be at compile time readonly int a can use default value, without initializing initialization can be at run time 二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值.而static readonly是在运行时计算出其值的,所以还可以通过静态构造

const和static readonly 区别

const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly MyClass myins = new MyClass();(对) static readonly MyClass myins = "3";(对) const string myins = "3";(对) const MyClass myins = new MyClas

浅谈C#中const、static readonly区别

const 编译时常量 static readonly 运行时常量 直接上代码 1.新建一个类库BLL->添加类Teacher 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace BLL 8 { 9 public class Teacher 10 { 11 public

c#中const、static、readonly的区别

1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数中初始化. 因此,根据所使用的构造函数,readonly 字段可能具有不同的值. const只能在初期就使用常量初始化好.对于每一次编译后的结果,const的值是固定的,而readonly的值是可以在运行的时候才确定值的. 2. const 与 static static 定义的是静态变量.可以在外

static, readonly, const

static Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it canno

【Unity|C#】基础篇(6)——const、readonly、static readonly

[学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.com/s/1mhOmBG0 [内容] const readonly static readonly 三者比较 [笔记] const 编译时常量 类的常量 只能在声明时赋值 readonly 运行时常量 对象的常量 可以在 声明时 赋值(与常量一样),或 构造函数中 赋值 static readonly 运行时

《OOC》笔记(1)——C语言const、static和extern的用法

<OOC>笔记(1)——C语言const.static和extern的用法 C语言中const关键字用法不少,我只喜欢两种用法.一是用于修饰函数形参,二是用于修饰全局变量和局部变量. 用const修饰的函数形参 直接修饰 一个形如 int Minus(const int a, const int b, int testCase); 的函数,const的意义是什么呢? 答:参数a被const修饰,说明在Minus函数内,编译器不允许a被别的变量(例如x)赋值(修改).参数b同理. 如果你写了a

C# const与static的理解

C#  const与static的理解 static readonly与 const变量,作用是一样的,无论访问修饰符是不是public,还是其它(private. protected.internal),变量名称一般为大写,中间以下划线. 例如: public static readonly int Page_Size=  10; public const int Page_Size= 10;