C# – What is readonly variable and how to use it

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.

You can assign a value to a readonly field only in the following contexts:

When the variable is initialized in the declaration, for example:

public readonly int y = 5;

For an instance field, in the instance constructors of the class that contains the field declaration, or for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a readonly field as an out or ref parameter.


// Readonly fields

using System;
public class ReadOnlyTest
class MyClass
public int x;
public readonly int y = 25; // Initialize a readonly field
public readonly int z;
public MyClass()
z = 24; // Initialize a readonly instance field

public MyClass(int p1, int p2, int p3)
x = p1;
y = p2;
z = p3;

public static void Main()
MyClass p1= new MyClass(11, 21, 32); // OK
Console.WriteLine(“p1: x={0}, y={1}, z={2}” , p1.x, p1.y, p1.z);
MyClass p2 = new MyClass();
p2.x = 55; // OK
Console.WriteLine(“p2: x={0}, y={1}, z={2}” , p2.x, p2.y, p2.z);

p1: x=11, y=21, z=32
p2: x=55, y=25, z=24

In the preceding example, if you use a statement like this:
p2.y = 66; // Error

you will get the compiler error message:
The left-hand side of an assignment must be an l-value
which is the same error you get when you attempt to assign a value to a constant.


  • The readonly keyword is different from the const keyword.
  • A const field can only be initialized at the declaration of the field.
  • A readonly field can be initialized either at the declaration or in a constructor.

Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks;

Share this:

C# – What is boxing and unboxing in .net it

Boxing_Unboxing-1When we move a value type to reference type the data is moved from the stack to the heap. When we move reference type to a value type the data is moved from the heap to the stack.

This movement of data from the heap to stack and vice-versa creates a performance hit.

When the data moves from value types to reference types its termed as ‘Boxing’ and the vice versa is termed as ‘UnBoxing’.

If you compile the above code and see the same in ILDASM you can see in the IL code how ‘boxing’ and ‘unboxing’ looks, below figure demonstrates the same.

Performance implication of Boxing and unboxing

In order to see how the performance is impacted we ran the below two functions 10,000 times. One function has boxing and the other function is simple. We used a stop watch object to monitor the time taken.

The boxing function was executed in 3542 MS while without boxing the code was executed in 2477 MS. In other words try to avoid boxing and unboxing. In project you always need boxing and unboxing , use it when it’s absolutely necessary.

Share this:

OOP – What is virtual method?

  • Virtual method is a method whose behavior can be overridden in derived class.
  • Virtual method allows declare a method in base class that can be redefined in each derived class.
  • When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
  • The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
  • By default, methods are non-virtual. You cannot override a non-virtual method.
  • You cannot use the virtual modifier with the static, abstract, private or override modifiers.
  • Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Share this:

OOP – What is static polymorphism?

  • The decision is made at compile time.
  • Which method is to be called is decided at compile-time only.
  • Method overloading is an example of this.
  • Compile time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.
  • Method overloading is a concept where a class can have more than one method with the same name and different parameters.
  • Compiler checks the type and number of parameters passed on to the method and decides which method to call at compile time and it will give an error if there are no methods that match the method signature of the method that is called at compile time.
Share this:

OOP – What is dynamic or runtime polymorphism?

  • Run-time polymorphism is achieved by method overriding.
  • Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.
  • By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.
  • Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by reference variable.
  • Compiler would not be aware whether the method is available for overriding the functionality or not. So compiler would not give any error at compile time. At runtime, it will be decided which method to call and if there is no method at runtime, it will give an error.

Share this: