What is a Static Class in C#?

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

  1. You will get an error if you declare any member as a non-static member. 
  2. 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.
  3. The static keyword is used before the class keyword in a class definition to declare a static class.
  4. 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.

Static Fields / Properties
The memory will be allocated individually, without any relation to the object.

Static Methods
These methods can only implement operations on static fields and properties and can‘t access non-static members.

Static Members in C# 

Demo on static members

The following code shows the difference between static and non-static members of a class. The Student class has non-static members, and their values are set by the program.

using System;  
using System.Linq;  
using System.Text;  
using System.Collections.Generic;  
namespace StaticConstructorsDemo  
{  
    class Student  
    {  
        //non-statcic data members  
        public string StudentName;  
        public string Course;  
        public void SetStudentDetails(string StuName, string Cou)  
        {  
            StudentName = StuName;  
            Course = Cou;  
        }  
        public void DisplayStudentDetails()  
        {  
            Console.WriteLine(StudentName + " - " + Course);  
        }  
  
        //static data members  
        public static string CollegeName = "ABC College of Technology";  
        public static string CollegeAddress = "Hyderabad";  
  
        //static methods  
        public static void DisplayCollegeDetails()  
        {  
            Console.WriteLine(CollegeName);  
            Console.WriteLine(CollegeAddress);  
        }  
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            //access static members  
            Student.DisplayCollegeDetails();  
  
            //acess non-static members  
            Console.WriteLine();  
            Student s1= new Student();  
            Student s2 = new Student();  
            s1.SetStudentDetails("Sarath","MCA");  
            s1.SetStudentDetails("Syam","MBA");  
            s1.DisplayStudentDetails();  
            s2.DisplayStudentDetails();  
            Console.Read();  
        }  
    }  
}

Static Constructors

A static constructor is used to initialize the static data members, whereas the normal constructor (non-static constructor) is used to initialize the non-static data members.

Syntax

static classname()  
{  
   //some code  
}

Rules

  1. Static constructors can‘t contain any access modifiers.
  2. Static constructors can‘t be defined with arguments.
  3. Static constructors can‘t access the non-static data members.

Demo on static constructors

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();  
        }  
    }  
}

Summary

This article showed how to create and use a static class in C#.


Recommended Free Ebook
Similar Articles