Open In App

C# | Static Class

Last Updated : 20 Sep, 2023
Like Article

In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members and static methods. It is not allowed to create objects of the static class and since it does not allow to create objects it means it does not allow instance constructor. Static classes are sealed, means you cannot inherit a static class from another class.


static class Class_Name
// static data members
// static method

In C#, the static class contains two types of static members as follows:

  • Static Data Members: As static class always contains static data members, so static data members are declared using static keyword and they are directly accessed by using the class name. The memory of static data members is allocating individually without any relation with the object.
static class Class_name 
public static nameofdatamember;

  • Static Methods: As static class always contains static methods, so static methods are declared using static keyword. These methods only access static data members, they can not access non-static data members. 
static class Class_name {
public static nameofmethod()
// code

Example 1: 


// C# program to illustrate the
// concept of static class
using System;
namespace ExampleOfStaticClass {
// Creating static class
// Using static keyword
static class Author {
    // Static data members of Author
    public static string A_name = "Ankita";
    public static string L_name = "CSharp";
    public static int T_no = 84;
    // Static method of Author
    public static void details()
        Console.WriteLine("The details of Author is:");
// Driver Class
public class GFG {
    // Main Method
    static public void Main()
        // Calling static method of Author
        // Accessing the static data members of Author
        Console.WriteLine("Author name : {0} ", Author.A_name);
        Console.WriteLine("Language : {0} ", Author.L_name);
        Console.WriteLine("Total number of articles : {0} ",


The details of Author is:
Author name : Ankita 
Language : CSharp 
Total number of articles : 84

Example 2:


// C# program to demonstrate
// the concept of static class
using System;
// declaring a static class
public static class GFG {
    // declaring static Method
    static void display()
        Console.WriteLine("Static Method of class GFG");
// trying to inherit the class GFG
// it will give error as static
// class can't be inherited
class GFG2 : GFG {
    public static void Main(String[] args) {

Compile Time Error:

prog.cs(20,7): error CS0709: `GFG2′: Cannot derive from static class `GFG’

Explanation: In the above example, we have a static class named as Author by using static keyword. The Author class contains static data members named A_name, L_name, and T_no, and a static method named as details(). The method of a static class is simply called by using its class name like Author.details();. As we know that static class doesn’t consist object so the data member of the Author class is accessed by its class name, like Author.A_name, Author.L_name, and Author.T_no.  

Difference between static and non-static class 

Static Class Non-Static Class
Static class is defined using static keyword. Non-Static class is not defined by using static keyword.
In static class, you are not allowed to create objects. In non-static class, you are allowed to create objects using new keyword.
The data members of static class can be directly accessed by its class name. The data members of non-static class is not directly accessed by its class name.
Static class always contains static members. Non-static class may contain both static and non-static methods.
Static class does not contain an instance constructor. Non-static class contains an instance constructor.
Static class cannot inherit from another class. Non-static class can be inherited from another class.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads