Static Class Example in C#


Today I will explain a Static Class Example in C#. To begin with, let us first understand what is a static class.

Static Class in C#

Basically, a static class in C# is a class, which can’t be instantiated. Therefore, the purpose of creating a static class is to define methods that can’t be associated with any object. Whenever we create a static class in a program, the compiler makes sure that the programmer doesn’t create an object of this class. Further, a static class can have static fields, static constructor, and static methods. In general, it can’t contain any instance member.

Program Demonstrating a Static Class Example in C#

The following program shows how to create a static class in C#. Also, the static class that we create in this example contains several static members. Accordingly, it contains a static constructor and two static methods.

Since the purpose of defining the class ImportantFunctions is just to create two functions, we should create this class as a static class. This class provides the function to compute factorial, and to compute the simple interest. Hence, the class doesn’t have any attributes and behavior. Moreover, objects are real-life entities that possess properties and have a behavior. Hence, we can’t create any object of the class ImportantFunctions.

Further, the static constructor present in this class just demonstrates how to create a static constructor. It also shows, when the compiler calls a static constructor. In fact, the compiler calls the static constructor implicitly when the program execution starts. Also, in the Main() method, if we create an object of this class, it will result in a compile error. The two static functions can be called by using the class name, and the dot operator.

using System;
namespace StaticClassDemo2
    class Program
        static void Main(string[] args)
            //  ImportantFunctions ob = new ImportantFunctions();

            int f = ImportantFunctions.Factorial(12);
            Console.WriteLine("Factorial =" + f);

            double si = ImportantFunctions.FindSimpleInterest(3500, 6.9, 3);
            Console.WriteLine("Simple Interest = " + si);

        static class ImportantFunctions
            static ImportantFunctions()
                Console.WriteLine("Inside Static Constructor");

            public static int Factorial(int n)
                    int fact = 1;
                    if (n == 0) return 1;
                    for(int i=1;i<=n;i++)
                        fact = fact * i;
                    return fact;

            public static double FindSimpleInterest(double p, double r, double t)
                double si = (p * r * t) / 100.0;
                return si;


The output of the Program Demonstrating a Static Class Example in C#
The output of the Program Demonstrating a Static Class Example in C#

Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface


You may also like...