'Static readonly' vs. 'const'

前端 未结 18 2589
旧巷少年郎
旧巷少年郎 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 04:43

    Const: Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default a const is static and we cannot change the value of a const variable throughout the entire program.

    Static ReadOnly: A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime

    Reference: c-sharpcorner

    0 讨论(0)
  • 2020-11-22 04:49

    One thing to note is const is restricted to primitive/value types (the exception being strings).

    0 讨论(0)
  • 2020-11-22 04:51

    Static Read Only:

    The value can be changed through a static constructor at runtime. But not through a member function.

    Constant:

    By default static. A value cannot be changed from anywhere (constructor, function, runtime, etc. nowhere).

    Read Only:

    The value can be changed through a constructor at runtime. But not through a member function.

    You can have a look at my repository: C# property types.

    0 讨论(0)
  • 2020-11-22 04:51

    Const: Constant variable values have to be defined along with the declaration and after that it won't change.const are implicitly static, so without creating a class instance we can access them. This has a value at compile time.

    ReadOnly: We can define read-only variable values while declaring as well as using the constructor at runtime. Read-only variables can't access without a class instance.

    Static readonly: We can define static readonly variable values while declaring as well as only through a static constructor, but not with any other constructor. We can also access these variables without creating a class instance (as static variables).

    Static readonly will be better choice if we have to consume the variables in different assemblies. Please check the full details in the below blog post:

    Const Strings – a very convenient way to shoot yourself in the foot

    0 讨论(0)
  • 2020-11-22 04:53

    This is just a supplement to the other answers. I will not repeat them (now four years later).

    There are situations where a const and a non-const have different semantics. For example:

    const int y = 42;
    
    static void Main()
    {
      short x = 42;
      Console.WriteLine(x.Equals(y));
    }
    

    prints out True, whereas:

    static readonly int y = 42;
    
    static void Main()
    {
      short x = 42;
      Console.WriteLine(x.Equals(y));
    }
    

    writes False.

    The reason is that the method x.Equals has two overloads, one that takes in a short (System.Int16) and one that takes an object (System.Object). Now the question is whether one or both apply with my y argument.

    When y is a compile-time constant (literal), the const case, it becomes important that there does exist an implicit conversion from int to short provided that the int is a constant, and provided that the C# compiler verifies that its value is within the range of a short (which 42 is). See Implicit constant expression conversions in the C# Language Specification. So both overloads have to be considered. The overload Equals(short) is preferred (any short is an object, but not all object are short). So y is converted to short, and that overload is used. Then Equals compares two short of identical value, and that gives true.

    When y is not a constant, no implicit conversion from int to short exists. That's because in general an int may be too huge to fit into a short. (An explicit conversion does exist, but I didn't say Equals((short)y), so that's not relevant.) We see that only one overload applies, the Equals(object) one. So y is boxed to object. Then Equals is going to compare a System.Int16 to a System.Int32, and since the run-time types do not even agree, that will yield false.

    We conclude that in some (rare) cases, changing a const type member to a static readonly field (or the other way, when that is possible) can change the behavior of the program.

    0 讨论(0)
  • 2020-11-22 04:54

    const:

    1. value should be given upon declaration
    2. compile time constant

    readonly:

    1. value can be given upon declaration or during runtime using constructors.The value may vary depend upon the constructor used.
    2. run time constant
    0 讨论(0)
提交回复
热议问题