Const vs Static vs Readonly
A variable declared
as const (read: constant) must be assigned a value at declaration,
and this value may not then change at a later time. The value in
a const variable is what's called a "compile-time" value,
and is immutable (which means it does not change over the life of
the program). Only primitive or "built-in" C# types (e.g. int, string, double) are allowed to be
declared const.You want to
use const when you have a variable whose value will not change, ever,
during the time your application is being used. Further, any variable declared
as const will also, implicitly, be declared static.
publicconst
string
ConnectionString
="YourConnectionString";
A static member (variable, method, etc) belongs to
the type of an object rather than to an instance of that type.
A readonly field is one where assignment to that
field can only occur as part of the declaration of the class or in a
constructor. This means that a readonly variable can have different value at
runtime. But once it is evaluated(at runtime), it will not change for the lifetime.
publicclass
TestClass
{
public
readonly
string
ConnectionString
="TestConnection";
public
TestClass()
{// This shows the ‘
ConnectionString’ can have different values depending upon where it is instantiated from.
ConnectionString
="DifferentConnection";
}
public
void
TestMethod
()
{
ConnectionString
="NewConnection";//Will not compile
}
}
Const
vs. Static Readonly
So, What exactly is
a static readonly :- It cannot be changed outside of its declaration or
containing class's constructor (due to readonly). It is part of the type,
not part of an instance of that type (due to static).
1.
a const variable is not a reference to
anything; it is literal value "burned" into the code (using a
constant is the true definition of hard coding a value). A static readonly
variable is a reference, and consequently a lookup is performed any time this
variable is accessed
2.
But there's another, more subtle difference that
we should be aware of. If a const variable exists in Assembly A and
is used in Assembly B, when Assembly A gets recompiled with a new value for
the const variable Assembly B will still have the previous value
until it is also recompiled.
1. If you know the
value will never, ever, ever change for any reason,
use const.
2. If you're unsure
of whether or not the value will change, but you don't want other classes or
code to be able to change it, use readonly.
3. If you need a
field to be a property of a type, and not a property of an instance of that
type, use static.
4. A const value is
also implicitly static.
|
Constants:
1. Constants can be assigned values only
at the time of declaration
2. Constant variables have to be
accessed using "Classname.VariableName"
3. Constants are known at compile time
|
Read Only:
1. Read only variables can be assigned
values either at runtime(i.e. from user) or at the time of instance
initialization via constructor
2. Read only variables have to be
accessed using the "InstanceName.VariableName"
3. Read only variables are known at run
time.
|
No comments:
Post a Comment