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.
Leave a Reply