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.

What is a field ?



A field to start with is another word for a variable in a class.
Static variables are initialized when the class is loaded first. An int is given an initial value of  zero and a bool False.

What is a constant ?



A constant or const variable behaves as a variable. We give it an initial value and can use it wherever we can use a variable.
Unlike a variable, we are not allowed to change the value of a const. The change is an assignment statement.  
We have to initialize the const variable at the time of creation. We are not allowed to initialize it later in our program. A constant is static by default and we cannot use the instance reference

What is a sealed class ?



Sealed is another modifier that applies to classes. aaa is a sealed class. No class can derive from aaa. In another words aaa cannot act as a base class for any class.

What is a class ?

A class is also called a data structure. It consists of data members like constants, fields and events and function members like methods, properties, indexers, operators, constructors, static constructors and destructors. A class within a class is called a nested class. Thus we can place 11 different types of entities in a class. Function members are the only members of a class that contain executable code. A class creation creates a new declaration space.

All classes derive from object . Object is the mother of all classes.