In this article, I will demonstrate you all about static class with example. I will also let you know why we use the static class in place of normal class.

What is Static Class?

Static Class is a special type of class that is used to access the member functions without creating the object of the class. We can access static class members using the name of the class. The static keyword is used to create a static class. If you create a static class, all its members would be static.

Why to use static class?

If there is a requirement that we don’t need to create the instance of the class but we want to access their class members, then we create class as static.

Features of Static Class

Static class contains only static members.

You cannot create the instance of the static class.

Static class can’t be inherited.

Example of Static Class

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Runtime.InteropServices;  
using System.Globalization;  
  
namespace ConsoleApplication2  
{  
    public static class Square  
    {  
        public static double side;  
  
        public static double Perameter()  
        {  
            return side * 4;  
        }  
  
        public static double Area()  
        {  
            return side * side;  
        }  
    }  
  
    public class Math  
    {  
        public static void Main()  
        {  
            Square.side = 20.02;  
            Console.WriteLine("The side of square is " + Square.side);  
            Console.WriteLine("The perameter of square is " + Square.Perameter());  
            Console.WriteLine("The area of square is " + Square.Area());  
            Console.ReadLine();  
        }  
    }  
}  

 

Output

Csharp static class

Thanks for reading this article. Hope you enjoyed it.