'Static readonly' vs. 'const'

前端 未结 18 2592
旧巷少年郎
旧巷少年郎 2020-11-22 04:07

I\'ve read around about const and static readonly fields. We have some classes which contain only constant values. They are used for various things

18条回答
  •  伪装坚强ぢ
    2020-11-22 05:10

    const and readonly are similar, but they are not exactly the same.

    A const field is a compile-time constant, meaning that that value can be computed at compile-time. A readonly field enables additional scenarios in which some code must be run during construction of the type. After construction, a readonly field cannot be changed.

    For instance, const members can be used to define members like:

    struct Test
    {
        public const double Pi = 3.14;
        public const int Zero = 0;
    }
    

    Since values like 3.14 and 0 are compile-time constants. However, consider the case where you define a type and want to provide some pre-fab instances of it. E.g., you might want to define a Color class and provide "constants" for common colors like Black, White, etc. It isn't possible to do this with const members, as the right hand sides are not compile-time constants. One could do this with regular static members:

    public class Color
    {
        public static Color Black = new Color(0, 0, 0);
        public static Color White = new Color(255, 255, 255);
        public static Color Red   = new Color(255, 0, 0);
        public static Color Green = new Color(0, 255, 0);
        public static Color Blue  = new Color(0, 0, 255);
        private byte red, green, blue;
    
        public Color(byte r, byte g, byte b) => (red, green, blue) = (r, g, b);
    }
    

    But then there is nothing to keep a client of Color from mucking with it, perhaps by swapping the Black and White values. Needless to say, this would cause consternation for other clients of the Color class. The "readonly" feature addresses this scenario.

    By simply introducing the readonly keyword in the declarations, we preserve the flexible initialization while preventing client code from mucking around.

    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, green, blue) = (r, g, b);
    }
    

    It is interesting to note that const members are always static, whereas a readonly member can be either static or not, just like a regular field.

    It is possible to use a single keyword for these two purposes, but this leads to either versioning problems or performance problems. Assume for a moment that we used a single keyword for this (const) and a developer wrote:

    public class A
    {
        public static const C = 0;
    }
    

    and a different developer wrote code that relied on A:

    public class B
    {
        static void Main() => Console.WriteLine(A.C);
    }
    

    Now, can the code that is generated rely on the fact that A.C is a compile-time constant? I.e., can the use of A.C simply be replaced by the value 0? If you say "yes" to this, then that means that the developer of A cannot change the way that A.C is initialized -- this ties the hands of the developer of A without permission.

    If you say "no" to this question then an important optimization is missed. Perhaps the author of A is positive that A.C will always be zero. The use of both const and readonly allows the developer of A to specify the intent. This makes for better versioning behavior and also better performance.

提交回复
热议问题