Classes are the irreplaceable ingredient of any .NET assembly. A Class is the generic definition of what an object is a template. The keyword class in C# indicates that we are going to define a new class (type of object)
A class declaration consists of class declaration and class body. Class header includes attributes, modifiers, "class" keyword, the class name and the base class list.
attribute accessibility modifiers class classname: baselist
// Class Body
Lets see what each of these header means.
attribute describes the class. eg. Serializable attribute identifies a class that can be serialized to store physically on the disk. We have freedom to define our own custom attributes.
accessibility describes the visibility of the class. eg. Public, Internal etc. Public classes are visible in the current assembly as well as the assembly referencing it. By default if no accessibility is defined, Internal is the default class accessiblity that means this class will only be visible in the current assembly.
modifiers filters the declaration of the class. eg. Sealed keyword prevent the class to be inherited.
||A class can't be inherited|
||Instance of the class can't be created|
||Class can only contain static members (methods, variable etc.)|
||The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.|
class is the keyword that is used to define a class.
classname is the string that is used to name a class we are going to define.
baselist is the list of class or interface that we are going to inherit in this class. C# doesn't support multiple inheritence so we can only inherit one class; however, we can inherit multiple interface. In case of inherting multiple inheritence we can write them as comma separated. If no class is inhertited, by default a class inherit System.Object.
A class can contain members in the form of member function or data member. Members can be of several types eg. Methods, Properties, Fields, Constants, Constructors etc.
public class myClass
int i = 0;
protected string GetMyName()
We can define the visibility of the member using accessibility keywords. Private is the default accessibility for the class member.
||Member is visible in the containing assembly only|
||Member is visible in the containing assembly or visible inside current class and any descendants|
||Member visible inside the current class|
||Member visible inside current class and any descendants|
||Member visible in current assembly as well as other assembly referencing this assembly|
Members can contain the modifiers as well that refine the definition of the member. These modifiers are optional and there are no default modifiers. Following is the list of modifiers that are frequently used.
||Member function has no implementation and is implemented through inheritence|
||Hides the similar member or member in the base class|
||Used to override a virtual member in the base class|
||Can't be set its value except at the time of declaration or in the constructors|
||This member can't be inherited|
||This member belongs to the class not to the instance of the class|
||This member is overridable in the derived class.|
Hope this will be helpful in understanting about a class. Thanks for reading.