zoukankan      html  css  js  c++  java
  • [C# FAQ]const和static readonly的区别

     

    名词解释

    const:  该关键字用于修改字段或局部变量的声明。 它指定字段或局部变量的值是常数,不能被修改。

    const必须在声明时赋值,对于引用类型的常数,可能的值只能是 string 和 null。

    你还可以像这样赋值:

       1: // 一次给多个常量赋值
       2: public const double x = 1.0, y = 2.0, z = 3.0;
       3:  
       4: // 用常量参与常量赋值
       5: public const int c1 = 5;
       6: public const int c2 = c1 + 100;

    但以下赋值方法是不允许的。

       1: // 调用一个函数来获得值:
       2: const int i = GetValue();
       3:  
       4: // 涉及到了构造函数:
       5: const SomeClass myObj = new SomeClass();

    ReadOnly:  该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中。

       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: }

    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#规范中给出的例子:

    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,那么后面的两个语句就都会出错。

    小结

    1. Const的好处
      1. 能够带来稍许的性能上的提升。
      2. 能够给一些“神奇”的值命名。在程序中出现的数值等“神奇”的数值,用Const替换会使程序更容易阅读。
    2. Const的坏处
      1. 版本问题。如果A.exe调用了B.dll中定义的const常量,那么如果重新调整const常量时,A.exe中获取的常量值仍然是修改前的内容。更多看这里
    3. readonly的好处
      1. 在初始化和构造函数前可以赋值,可以使用方法为其赋值。
      2. 使用场合更多。
  • 相关阅读:
    linux/windows nginx安装
    linux/windows vsftpd安装
    linux 操作命令
    linux/windows java tomcat安装
    常见的Activity Action Intent常量
    Intent.ACTION_PICK
    Android实现抽奖转盘
    Android-短信验证
    Android-多平台分享(新浪微博)
    Android 手势滑动,多点触摸放大缩小图片
  • 原文地址:https://www.cnblogs.com/tukzer/p/2140202.html
Copyright © 2011-2022 走看看