C# | Static Class - GeeksforGeeks

22 Jul, 2021
In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members, static methods, and a static constructor.It is not allowed to create objects of the static class. Static classes are sealed, means you cannot inherit a static class from another class.
Syntax: 
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.Syntax: 
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. Syntax: 
static class Class_name {

    public static nameofmethod()
    {
         // code 
     }
}
Example 1: 
C#
// C# program to illustrate the// concept of static classusing System; namespace ExampleOfStaticClass { // Creating static class// Using static keywordstatic 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 Classpublic class GFG {     // Main Method    static public void Main()    {         // Calling static method of Author        Author.details();         // 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} ",                                              Author.T_no);    }}}
The details of Author is:
Author name : Ankita 
Language : CSharp 
Total number of articles : 84
 
Example 2:
C#
// C# program to demonstrate// the concept of static classusing System;  // declaring a static classpublic 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 inheritedclass 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.  
 
sumitgumber28
CSharp-OOP
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Method Overriding
C# Dictionary with examples
Difference between Ref and Out keywords in C#
Top 50 C# Interview Questions & Answers
Introduction to .NET Framework
C# | Constructors
C# | Abstract Classes
C# | String.IndexOf( ) Method | Set - 1
Different ways to sort an array in descending order in C#
C# | Arrays