What is a static class in C#?

Classes can be defined as user-defined data types representing an object’s state (properties) and behavior (actions).

Types of classes

There are four major types of classes in C#:

  1. Abstract class
  2. Partial class
  3. Sealed class
  4. Static class
Types of classes in C#
Types of classes in C#

Static class

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.

Example

The following code will help understand the use of the static class:

using System;
namespace ExampleOfStaticClass {
// Creating static class
// Using static keyword
static class Circle {
//static variable that contains the value of pi
public static double Pi = 3.14;
//static method that takes the radius of the circle as parameter and returns the area
public static double Area(double radius) {
return Pi * radius * radius;
}
}
// Driver Class
class 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 object
double area = Circle.Area(radius);
Console.WriteLine("Area of circle is: " + area);
}
}
}

Explanation

  • Line 7: We declare a class Circle using the static keyword.
  • Line 8: Declared a static variable Pi.
  • Lines 13–14: Declared a method Area that will return the area of the circle.
  • Line 23: Calculate the circle area by calling the static method of Circle without creating its object.
  • Line 24: Print the area of circle on the console.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved