Static Class is a special kind of class that can't be instantiated means we can't create an instance of the static class in C# language. It can only contain static members like static methods, static constructors, static property, etc... A static class is a sealed class that can't be inherited from another class. It can only have a static constructor and an instance constructor can't be created for a static class.
Static class can be created using static keyword in C# and below is an example of the same.
As you can see above all the methods, properties are having static keywords before data type.
The following list provides features of static class
- It contains only static members.
- An instance of the static class can't be created as we do for the normal class using a new keyword.
- It cannot have an Instance Constructor.
We can use the static members in static class like below
C# language provides many predefined static classes from its library for usage in .Net application and System.Math is one of them.
We can create a static class for any custom requirement in our application and the main usage of static class is to do prefined calculation or doing some utility for the application for example we get EnvironmentName of the application using static class.
Abstract class in C# is used only to be base class of other class which means all the abstract members must be implemented by the non-abstract class.
Like static class instances of the abstract class can't be created.
The following list provides features of the Abstract class
- The instance of Abstract Class can't be created.
- Abstract class must contain at least one abstract member.
- Mostly used for the Inheritance as a base class.
- It can have one or more non-abstract or completed methods.
Above is the simple abstract class named Shape which has abstract method GetArea so any derived class which is inheriting Shape abstract class will need to implement GetArea() method like below
Abstract class in C# is basically used to force the derived class to enforce the same hierarchies or standards.
An abstract class is very useful where various derived class requires implementation of the same kind or use common behavior or status.
Interface in C# is like a contract where we have only method signature and don't have the implementation of the methods.
C# language doesn't support multiple inheritance but one class can inherit multiple interfaces so interfaces are used for multiple inheritance.
The following list provides features of an Interface.
- No implementation is defined in Interface.
- All the members are public.
- Classes can inherit multiple interfaces.
- A class inheriting an interface must implement all the methods of the interface otherwise we will get a compile-time error.
As you can see above Student class implement IStudent interface and implements the method GetTotalMarks().
Hope you understood the above important concepts in C# Language