1 thought on “What is difference between Read only and static?”
Dev
const – The const keyword is used to modify a declaration of a field or local variable.
It specifies that the value of the field or the local variable is constant, which means it cannot be modified.
the const value MUST be declared/and know at compile time. you cannot change one throu runtime. it use for:
some strings displayed for the user, unchanged values like const double PI = 3.14; etc.
static – Use the static modifier to declare a static member, which belongs to the type itself rather
than to a specific object. The static modifier can be used with classes, fields, methods, properties,
operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes.
For example. it’s useful for helpful classes like MathHelper. where you can define public static
double Sum(double x, double y). in this case you don’t have to create instance of MathHelper class,
simply calls MathHelper.Sum(5, 6);
readonly – The readonly keyword is a modifier that you can use on fields. When a field declaration
includes a readonly modifier, assignments to the fields introduced by the declaration can only occur
as part of the declaration or in a constructor in the same class. it’s helpful when you want to block
modifying the field after class initialization.
*) readonly variable can take reference type value like.
Example
const int xt = 34;//const are bydefault static we can not make static to const
static readonly int ur = 23;//we can make static to readonly variable.
readonly int ty = xt;
//const string west=ty;
const variable can not take reference type we try to asign them compile time error
will come
*)readOnly variables value we can change within constructor only.
const variable value we can not change anywhere.
* const are by default static
example.
if we try to make static like below
static const int i=10;
them error will come.but in case of readonly variables we can make its static like
static readonly int x=20;
const – The const keyword is used to modify a declaration of a field or local variable.
It specifies that the value of the field or the local variable is constant, which means it cannot be modified.
the const value MUST be declared/and know at compile time. you cannot change one throu runtime. it use for:
some strings displayed for the user, unchanged values like const double PI = 3.14; etc.
static – Use the static modifier to declare a static member, which belongs to the type itself rather
than to a specific object. The static modifier can be used with classes, fields, methods, properties,
operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes.
For example. it’s useful for helpful classes like MathHelper. where you can define public static
double Sum(double x, double y). in this case you don’t have to create instance of MathHelper class,
simply calls MathHelper.Sum(5, 6);
readonly – The readonly keyword is a modifier that you can use on fields. When a field declaration
includes a readonly modifier, assignments to the fields introduced by the declaration can only occur
as part of the declaration or in a constructor in the same class. it’s helpful when you want to block
modifying the field after class initialization.
*) readonly variable can take reference type value like.
Example
const int xt = 34;//const are bydefault static we can not make static to const
static readonly int ur = 23;//we can make static to readonly variable.
readonly int ty = xt;
//const string west=ty;
const variable can not take reference type we try to asign them compile time error
will come
*)readOnly variables value we can change within constructor only.
const variable value we can not change anywhere.
* const are by default static
example.
if we try to make static like below
static const int i=10;
them error will come.but in case of readonly variables we can make its static like
static readonly int x=20;