3

C# related

Scenario 1: readonly keyword field/data member in a class will be initialized only once. So multiple instance of that class would exist with different values of that readonly data member. Methods and properties within that class will never modify this data member.

Scenario 2: const keyword field/data member in a class will be declared and initialized only once within same statement. Otherwise compiler generates error. In this case data member marked as const will hold the same value for multiple instances created for this class.

scenario 2 can also be achieved using static variable.

Then why there was a need to have const keyword in C#?

Sam
  • 73
  • 7

1 Answers1

3

Static variables can be changed at any time - they are just singletons within the class type.

Read-only variables can only be set once, at runtime. But they are read from the assembly each time they are accessed. They can be static or per instance values.

Consts are compile time constants. They are not evaluated at runtime, but when the client assembly is compiled. They may be inlined into your code by the compiler if it wishes to.

Consts can also be used within methods, so their value is only available within the scope of the method (unlike read only and static)

Const, read only and static are therefore all significantly different from each other.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • Presumably, the reason for in-lining a const is to improve performance, compared to readonly? – Polyfun Jul 29 '15 at 15:08
  • 1
    Yes, a const value will almost always end up in the instruction stream that the processor executes. A readonly variable will require a memory access. Pretty big difference when you count nanoseconds. Using a *public* const is however a very evil micro-optimization. – Hans Passant Jul 29 '15 at 15:21
  • @Jason,say a static variable is given a value x. after 10 mins in execution the value is changes to y. then after modification value y will be used by all instances and whatever decisions taken when value was x would be void. is there a practical use of this? – Sam Jul 29 '15 at 15:31
  • @Sam, a static variable means there is one "global" variable shared by all instances of the class, rather than every instance having its own private copy. – Jason Williams Jul 29 '15 at 22:50
  • 1
    ...So imagine every instance you create of your class needs a unique ID. With a static variable this is easy. The 'Next unique ID' starts at 1 and is incremented each time a new ID is needed - all instances of the class see the same number for the "next ID" that will be used. Each object might then have a non-static member variable to store its _own_ unique ID, so in its constructor it sets its unique ID from the static variable, and then increments the static variable, ensuring that every new instance gets a different and unique ID. – Jason Williams Jul 29 '15 at 22:51
  • ...other common examples are caches of information that needs to be shared by all the instances of a class, or singletons (things like factory classes, where you only ever want one instance of the class to be created) – Jason Williams Jul 29 '15 at 22:56
  • nice example, thanks. – Sam Jul 30 '15 at 13:57