Classes can be defined as user-defined data types representing an object’s state (properties) and behavior (actions).
There are four major types of classes in C#:
It is a type of class similar to a sealed class but has a static
data member, static constructor, and static method. One can generate a static class by using the static
keyword.
A static class can not be inherited by another class, and we can not create its object.
The following code will help understand the use of the static class:
using System;namespace ExampleOfStaticClass {// Creating static class// Using static keywordstatic class Circle {//static variable that contains the value of pipublic static double Pi = 3.14;//static method that takes the radius of the circle as parameter and returns the areapublic static double Area(double radius) {return Pi * radius * radius;}}// Driver Classclass Program {static void Main(string[] args) {double radius = 5;//calculating the area of the circle by calling the static method of Circle class without creating its objectdouble area = Circle.Area(radius);Console.WriteLine("Area of circle is: " + area);}}}
Circle
using the static
keyword.Pi
.Area
that will return the area of the circle.Circle
without creating its object.Free Resources