Static class

Author posted by Jitendra on Posted on under category Categories c# with Leave a comment on Static class

Example of Static Class, its usage and benefits

C# introduced a new feature known as Static Class.

A class which has only Static Methods is known as Static Class.

Benefit of Static Class:

  • Object of static class cannot be created.
  • All methods and members can be accessed by class Name.

Note :

  • All methods of static class must be specified explicitly by keyword static.
  • Static class cannot have instance constructor but it can have Static Constructor.

Example:


using System;
static class Shape
 {
    public static double GetArea(double Width, double height)
    {
        return Width * Height;
    }
 }

class Ractangle
{
   private void GetRactangleArea()
   {
     Double Area;
     Area = Shape.GetArea(10, 5);
   }
 }

Explanation:

In above Example Shape is Static class and contains static method named GetArea().

And other class (Rectangle) access the GetArea() method without creating the instance of class Shape.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *