Static class

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.

Posted

in

by

Tags:


Related Posts

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading