Wednesday, September 28, 2005

Difference between ReadOnly and Constant in C#



If you are using constant value you have intitalize that timeitself,but readonly filed u can initialize that timeitself or else in constructor.

Constant value is assigned in Compile time
readonly value is assigned in Run time


using System;

namespace difference
{

class read
{
public readonly int d = DateTime.Now.Hour;
public const int c = 10;

public read(int x)
{
d = x;

}
public read(int x,int y)
{
d = 12;
}



}
class Class1
{

static void Main(string[] args)
{
read obj = new read(DateTime.Now.Hour);
Console.WriteLine(obj.d);
Console.ReadLine();
}
}
}

No comments: