In C#, a static class is a class that cannot be instantiated. The main purpose of using static classes in C# is to provide blueprints of its inherited classes. A static class is created using the static keyword in C# and .NET. A static class can contain static members only. You can‘t create an object for the static class.
Advantages of Static Classes
- You will get an error if you declare any member as a non-static member.
- When you try to create an instance to the static class, it again generates a compile time error because the static members can be accessed directly with their class name.
- The static keyword is used before the class keyword in a class definition to declare a static class.
- Static class members are accessed by the class name followed by the member name.
Syntax of static class
static class classname
{
//static data members
//static methods
}
Static Class Demo
The following code declares a class MyCollege with the CollegeName and the Address as two static members. The constructor of the class initializes these member values, and in the Main method of the program, we call these static members to display their values.
namespace StaticConstructorsDemo
{
class MyCollege
{
//static fields
public static string CollegeName;
public static string Address;
//static constructor
static MyCollege()
{
CollegeName = "ABC College of Technology";
Address = "Hyderabad";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MyCollege.CollegeName);
Console.WriteLine(MyCollege.Address);
Console.Read();
}
}
}
Static Members
There are two types of C# static class members, static and non-static.
Non-static members
This is the default type for all the members. If you do not use the "static" keyword to declare a field/property or a method, then it can be called a "Non-static member." The main feature of a non-static member is it will be bound to the object only.
Non-static Fields / Properties
The memory is allocated when the object is created.
Non-static Methods
These methods can implement operations on non-static fields and properties
Static Members
If you use the "static" keyword for the declaration of a field/property or a method, it is called a "Static member". The main feature of a non-static member is that it will not be bound to any object. Instead, it is individually accessible with the class name. In other words, the static members are accessible directly without creating one object.

Join the conversation! Your thoughts help the community grow.