Constant
Constants are immutable values that are known at compile-time and do not change for the life of the program. Constants are declared with the const modifier. Only the C# built-in types (excluding System.Object) may be declared as const.
- The const modifier is used to declare fields or local variables that can not be changed
- These variables must be given initial values when they are declared.
- Const implies static.
- You have to initialize const variables while declaration.
- You cannot reassign a const variable.
- The compiler evaluates the const variables.
- The static modifier is not allowed in a constant declaration.
- A const field of a reference type other than string can only be initialized with null.
Example
class Constclass
{
public const int firstValue = 50;
public const string secondValue = "mithilesh";
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0},{1}", Constclass.firstValue, Constclass.secondValue);
Console.ReadLine();
}
}
OUTPUT:50 ,mithilesh
ReadOnly
The readonly keyword is a modifier that you can use on fields when a filed 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.
- You can create a read-only field in a class by declaring it as readonly.
- A readonly field can be initialized but not changed after that.
- readonly keyword applies only to fields not local variables
- We can assign readonly field at the time of declaration or in a constructor, not in any other methods.
- dynamic memory allocated for readonly fields and we can get the value at run time.
- Readonly belongs to the object created so accessed through the only instance of a class. To make it class member we need to add static keyword before readonly.
- The value may be different depending upon constructor used (as it belongs to object of the class)
- If you declare a non-primitive type (reference type) as readonly only reference is immutable not the object it contains.
- Since the value is obtained at run time, there is no dll versioning problem with readonly fields/ properties.
- We can pass readonly field as ref or out parameters in the constructor context.
public class Readonlyclass
{
class readclass
{
public int x;
public readonly int y = 25;
public readonly int z;
public readclass()
{
z = 24;
}
public readclass(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
}
static void Main(string[] args)
{
readclass rc = new readclass(11, 21, 32);
Console.WriteLine("Result1: x={0},y={1},z={2}", rc.x, rc.y, rc.z);
readclass rc2 = new readclass();
rc2.x = 50;
Console.WriteLine("Result2: x={0},y={1},z={2}", rc2.x, rc2.y, rc2.z);
Console.ReadLine();
}
}
OUTPUT:Result1: x=11,y=21,z=32
Result2; x=50,y=25,z=24
What is the difference between constant, readonly and static?
Constant
We need to provide the value to the const field when it is defined. The compiler then saves the constant’s value in the assembly’s metadata. This means that a constant can be defined only for the primitive type like boolean, char, byte and so on. Constants are always considered static members, not instance members.
Readonly
Readonly fields can only be resolved at runtime. That means we can define a value for a value using the constructor for the type in which the field is declared. The verification is done by the compiler that readonly fields are not written to by any method other than the constructor.
Static:
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.
No comments:
Post a Comment