Interviews - C# Corner

Surya Prakash  Pandey
Can we make readonly and const as static in C# ?
By Surya Prakash Pandey in C# onMay 15 2017
  • Madan Rout
    Oct, 2018 21

    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.Readonly : Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even a method. Readonly can set value in non static constructor 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.

    • 4
  • sushil kumar
    Mar, 2019 11

    we can make readonly as static but cannot make const as static bcz by default const is static.

    • 1
  • Surya Prakash  Pandey
    May, 2017 15

    Yes , we can make readonly as satatic but not to const . if we make readonly as static so while changing the variable value we need the static constructor as well .Thanks, Surya Prakash Pandey,

    • 1

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS