C# Const and Read Only Difference

We have const variables and read only variables. Is there a difference between them?
There is.

When you declare a const variable it has to be given a value at the time of declaration.

public const int a = 5;

When you declare read only variables you can give a value at the run time. You don’t have to give the value at the time of declaration. But the thing is you have to give the value in the constructor. You cannot give a value anywhere else.

Calss read
public readonly int b;

public read(int a)
  b = a ;


Const variables are by default static variables.


Popular posts from this blog

Error message “User does not have send-as privilege.” when sending emails programmatically in CRM 2011