Explain Classes in detail

A class is the blueprint from which individual objects are created. This blueprint describes the state and behaviour that all the objects of the class share.  A class may be composed of any number of members (such as properties, methods, and events) and data fields. A class has both an interface and a structure.

Concrete classes


A concrete class is a class that can be instantiated.

Abstract Classes


Abstract classes are intended to define common behaviours for derived types. An abstract class is designed only as a parent class from which child classes may be derived. Abstract classes cannot be instantiated. They are often used to represent abstract concepts or entities. The incomplete features of the abstract class are then shared by a group of subclasses which add different variations of the missing pieces. The behaviours defined by such a class are "generic" and much of the class will be undefined and unimplemented. Before a class derived from an abstract class can become concrete, it must implement all of the abstract methods in parent classes.

Abstract Method


An abstract method contains no body and is, therefore, not implemented by the base class. Thus, a derived class must override it. An abstract method is automatically virtual, and it is an error to use virtual and abstract together. The abstract modifier can only be used on normal methods. It cannot be applied to static methods. Properties and indexers can also be abstract.

When a derived class inherits an abstract class, it must implement all of the abstract methods in the base class. If it doesn't  then the derived class must also be specified as abstract. Thus, the abstract attribute is inherited until such time that a complete implementation is achieved.                                                                                              

Sealed classes


A sealed class cannot be used as a base class. For this reason, it also cannot be an abstract class. Sealed classes are primarily used to prevent derivation. They add another level of strictness during compile-time, improve memory usage, and trigger certain optimizations that improve run-time efficiency.  

Partial classes 


The partial keyword allows the class, struct, method or interface to span across multiple files. Partial classes are classes that can be split over multiple definitions (typically over multiple files), making it easier to deal with large quantities of code. At compile time the partial classes are grouped together, thus logically make no difference to the output. A primary benefit of partial classes is allowing different programmers to work on different parts of the same class at the same time. They also make automatically generated code easier to interpret, as it is separated from other code into a partial class. 

Static Classes      


When a class is defined as static, it cannot be instantiated using the new keyword, and it can contain only static members or fields. 

Static classes are used to create data and functions that can be accessed without creating an instance of the class. Static classes can be used when there is no data or behavior in the class that depends on object identity.

It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework Common Language Runtime (CLR) when the program or namespace containing the class is loaded


Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state. The System.Console and System.Math classes are good examples of static classes.
The main features of static classes are: 

  • They only contain static members.
  • They cannot be instantiated. 
  •  They are sealed.
  • They cannot contain Instance Constructors.

Static Members

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called.

Static methods can be overloaded but not overridden. A field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object instance is required.

C# does not support static local variables (variables that are declared in method scope).

No comments:

Post a Comment