What is the difference between const and readonly
When using the const keyword to set a constant variable, the value needs to be set at compile
time.
The const keyword may not be used with an object of reference type.
All
the constant variables are static ie they are shared across all the
instances of the class. You dont have to add the keyword "static".
In case a reference type needs to be assigned a non-changeable value, it may be set as readonly
Readonly fields are delayed initalized constants.
When we declare a field as const it is treated as a static field. where as the Readonly fields
are treated as normal class variables.
if we want a field that can have different values between different objects of same class, however
the value of the field should not change for the life span of object; We should choose the Read
Only fields rather than constants.Since the constants have the same value across all the
objects of the same class; they are treated as static.
What are value types and reference types
When
you declare a variable in a .Net application, it allocates some chunk
of memory into the RAM.
This memory has 3 things first the name of the
variable, second data type of the variable and finally the value of the
variable.
Depending on what kind of data type your variable is, the memory is
allocated to it.
There are two types of memory
allocation stack memory and heap memory.
Value
types are types which hold both data and the memory on the same
location.
While a reference type has a pointer which points to the
memory location.
Below is a simple integer data type with name
'i' whose value is assigned to an other integer data type with name 'j'.
Both these memory values are allocated on the stack.
When we
assign the 'int' value to the other 'int' value it creates a complete
different copy. In other word if you change either of them the other
does not change. These kinds of data types are called as 'Value types'.
public void method()
{
int i = 9;
int j=i;
}
When
we create an object and when we assign one object to the other object,
they both point to the same memory location as show in the below code
snippet.
So when we assign 'obj' to 'obj1' they both point to the same
memory location.
In other words if we change one of them the other object is also affected this is termed as 'Reference types'.
public void method()
{
Class1 obj = new Class1();
Class1 obj1 = obj;
}
In
.NET depending on data types the variable is either assigned on the
stack or on the heap.
'String' and 'Objects' are reference types and any
other .NET primitive data types are assigned on the stack.
What is boxing and unboxing
Boxing is used to store value types in the garbage-collected heap.
Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type.
Boxing a value type allocates an object instance on the heap and copies the value into the new object.
Consider the following declaration of a value-type variable:
int i = 123;
The following statement implicitly applies the boxing operation on the variable i:
// Boxing copies the value of i into object o.
object o = i;
The result of this statement is creating an object reference o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable.
Consider follwing C# code:
class TestBoxing
{
static void Main()
{
int i = 123;
// Boxing copies the value of i into object o.
object o = i;
// Change the value of i.
i = 456;
// The change in i does not effect the value stored in o.
System.Console.WriteLine("The value-type value = {0}", i);
System.Console.WriteLine("The object-type value = {0}", o);
}
}
/* Output:
The value-type value = 456
The object-type value = 123
*/
Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface.
An unboxing operation consists of:
Checking the object instance to make sure that it is a boxed value of the given value type.
Copying the value from the instance into the value-type variable.
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing