Thursday, February 17, 2011

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.

No comments:

Post a Comment

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...