this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Thursday, July 1, 2010

Declare Guid as Constant.

If you have tried to declare Guid constant with keyword, const you definitely come to error that Guid can’t be declared as constant.

We can only use Const keyword for the value type not for the Object type. Here Guid is object type. You can get to know your datatype is value type or object type by the color of it.

Like value type always shows in blue text(link) while reference type shows in skyblue.

We can use readonly keyword for object type.

We can declare constant of guid as

readonly Guid myId = Guid.NewGuid();

After all our goal is that nobody can change value of our variable and that can be achieved by above.

No comments: