Wednesday, October 29, 2014

What is readonly field in C# ?



Unlike a const, a readonly field does not have to be initialized at the time of creation.

A static readonly field can be initialized in a static constructor also. This is the major difference between a const and a readonly field. A static readonly field can be initialized in a static constructor also. This is the major difference between a const and a readonly field.

To sum up a readonly is a more generic const and it makes our programs more readable as we refer to a name and not a number.

A readonly field cannot be changed by anyone except a constructor.

C# does not permit a readonly as a parameter to a function that accepts a ref or a out parameters.

No comments:

Post a Comment