Static class in c#

 
We need an object of the class when we access the member of class. C# add a other concept for creating a class as Static. static class cannot be instantiated, there are two main features of a static class, one is no object of static class can be created and another is, a static class must contain only static members, then it is important that what is the main benefit to create a static class, the main benefit of making static class, we do not need to make any instance of this class, all members can be accessible with its own name.
 

Declaration

A static class is created by using keyword ‘Static’ as shown here:

Static class Clasname
{
//…..
}

 

The basic difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don’t need to use the new keyword to instantiate an instance of the class. you can access members of the static class by using the class name.
 
One more thing that is notable-within static class, all members must be explicitly specified as static, static class does not automatically make its members static. Static class can contain a collection of static methods.
 

Example

Shape is static class, it contain static function GetArea. In the Rectangle class GetArea function of the static shape class can be access without creating instance of Shape class.

using System;
static class Shape
{
  public static double GetArea(double Width, double height)
    {
      return Width * Height;
    }
}
class Rectangle 
{
   private void GetRactangleArea()
     {
        Double Area;
        Area = Shape.GetArea(10, 5);
     }
}

 
Although a static class cannot have an instance constructor, it can have a static constructor.