Const
By using const keyword you can define a constant field. Constants can be numbers, Boolean values, strings, or a null reference. Constant fields may not be modified. Constant value can not be change or modified with in the programme once it is allocated to a const field.
You can initialize const field at the time of declaration otherwise it will produce error (" The left-hand side of an assignment must be a variable, property or indexer"). That's why const field is like Compile Time Constant.
Example:
public class ConstExample
{
class SampleClass
{
public int x;
public int y;
public const int c1 = 5;
public const int c2 = c1 + 5;
public SampleClass(int p1, int p2)
{
x = p1;
y = p2;
//
c1 = 6; //Compile-Time Error
}
}
static void Main()
{
SampleClass objClass = new SampleClass(15, 20);
Console.WriteLine("x = {0}, y =
{1}", objClass .x, objClass .y);
Console.WriteLine("c1 = {0}, c2
= {1}",
SampleClass.c1, SampleClass.c2);
}
}
/*
Output
x = 15, y = 20
c1 = 5, c2 = 10
Readonly
By using readonly keyword you can define a readonly field. you can define values for your readonly field either at the time of declaration or with in constructor. this is the only difference between readonly and const keyword.
Note: Const field can only be initialized at the declaration of the field but readonly can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used
Example:--
class ReadonlyExample
{
public int x;
//
Initialize a readonly field at declaration
public readonly int y = 15;
public readonly int z;
public ReadonlyExample()
{
//
Initialize a readonly instance field in constructor
z = 24;
}
public ReadonlyExample(int p1, int p2, int p3)
{
x = p1;
y = p2;
z = p3;
}
}
static void Main()
{
ReadonlyExample p1 = new ReadonlyExample(11, 21, 32); // OK
Console.WriteLine("p1: x={0},
y={1}, z={2}", p1.x, p1.y, p1.z);
ReadonlyExample p2 = new ReadonlyExample();
p2.x = 55; // OK
Console.WriteLine("p2: x={0},
y={1}, z={2}", p2.x, p2.y, p2.z);
}
}
In above console examle you can see i initialize my readonly field in two ways . One is declaration time and other is within constructor.
Static
Static means that only one instance of a given variable exists for a class.Static value field initialize when programme execution start and its scope remains in the programme until the execution or programme stop.
Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors.
Note: Static value field same as constants but we can modify or perform operation on static field value like increment and decrement which we do not perform with const.
Example:
public class StaticExample
{
public static int i = 5;
public void test()
{
i=i+5;
Console.WriteLine(i);
}
}
public class Demo
{
static void Main()
{
StaticExample var = new StaticExample();
var.test(); // value of i=10 ( i=i+5)
StaticExample var1 = new StaticExample();
var1.test(); //value of i=15
Console.ReadKey();
}
}
in this example if you take a no static variable like
public int i = 5 it will print 10 both the times. because non-static variable lots its scope when new object creates, In other words it will initialize to zero after the current object execution stops.
0 comments:
Post a Comment