C# const, readonly, static readonly

转:

Const 定义的是静态常在对象初始化的时候赋值.以后不能改变它的值.属于编译时常量。不能用new初始化。

Readonly 是只读变量.属于运行时变量.可以在类constructor里改变它的值.不能作用于局部变量。

const 和 static 不能在一起用,它已经是静态的了。

我们都知道,const和static readonly的确非常像:通过类名而不是对象名进行访问,在程式中只读等等。在多数情况下能混用。
二者本质的差别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值

而static readonly,在程式中只读, 不过它是在运行时计算出其值的,所以还能通过静态构造函数来对它赋值,

readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员.


明白了这个本质差别,我们就不难看出下面的语句中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等其他类成员。

public class TestReadonly

{
    private int readonly int x; //X是只读字段
    public TestReadonly()
    {
        // 只能在初始化时,对只读字段赋值
        x = 100;
    }
     
    pubilc int GetX()
    {
        //这个语句是错误的,因为x不能被再次赋值,x是只读的(readonly),而
        // 下面的语句试图改变x的值。
        //x = x +100;
       
         
        //这个语句是正确的,因为语句执行后,x的值没有变
        int x1 = x + 100;
        return x1;
    }
}

因此,对于那些本质上应该是常量,不过却无法使用const来声明的地方,能使用static readonly。例如C#规范中给出的例子:

public class Color
{
    public static readonly Color Black = new Color(0, 0, 0);
    public static readonly Color White = new Color(255, 255, 255);
    public static readonly Color Red = new Color(255, 0, 0);
    public static readonly Color Green = new Color(0, 255, 0);
    public static readonly Color Blue = new Color(0, 0, 255);

    private byte red, green, blue;

    public Color(byte r, byte g, byte b)
    {
        red = r;
        green = g;
        blue = b;
    }
}


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

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

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

public static class test
{
public static readonly int ggg = 3;
static test() //public static ...报错 static构造函数不允许出现修饰符
{
ggg = 777;
}

}
public partial class MainWindow : Window
{
static readonly int g = 3;
const int k = 2;
readonly int i = 10;
public MainWindow()
{
// k = 88;//报错 值不可改变 编译时常亮 是静态常量 不能与static组合 MainWindow.k函数外可以这样访问
i = 999;
// g = 777;//报错 g为static修饰 是静态常量 不能再运行改变其值
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("hello world !"+" i="+i+" ggg= "+ test.ggg);//i=99 g=777

Window1 window = new Window1();

this.Close();
window.Show();
}

}

原文地址:https://www.cnblogs.com/bawang/p/8763302.html