Introduction
A static class is created using the "Static" keyword in C#. Static classes cannot be instantiated or inherited, but they can be accessed by static members only (static method, static variable, static constructor and etc..) but cannot be accessible to the non-static data members. We cannot create an instance for a static class.
Flow Chart
![Static Class And Static Class Members In C#]()
Figure. Static Class Members
How to create a static class?
- Create a class Name Details (or) keep it as you wish.
- Create the Static class by using the “Static” Keyword.
- Write the following code to create a static class
Syntax
How to Create a Static Method?
- Create the Static Method by using the “Static” Keyword.
- Create a Method Name Main (or) keep it as you wish.
- Write the following code to create a static Method
Syntax
How to create a Static Variable?
- Static variables can be initialized outside the member function or class definition.
- Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance.
Syntax
How to Create a Static Constructor?
- The static constructor does not take access modifiers.
- A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically
Syntax
Example
- Create a class Name Common (or) keep as it as you wish.
- Write the following code to create a static Class.
To create a method, call the Static class named Common and implement the method.
Write the following code.
Example
Output
![Static Class And Static Class Members In C#]()
Summary
In this article, I discussed the Static class and Static Members in C#. I have written this article focusing on beginners and students.