Saturday, October 31, 2009

Different Between Const and static readonly



Declaring with const or static readonly will give the same result, we will have uneditable variable.

The only one difference is that (const) must take its value at compile time, but (static readonly) will take its value in run time.

so we can get more benefit from static readonly by declare it in static constructor:


class Program
{
public static readonly Test test = new Test();

static void Main(string[] args)
{
test.Name = "Program";
}



0 comments:

Post a Comment