Skip to main content

How to Write a static Class in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Some code doesn't belong to any one object: converting a temperature, building a card, checking a value. In C#, a static class holds that kind of code. You call its members on the class itself, and C# won't let anyone create an object of it.

Syntax​

Syntax
static class <ClassName>
{
public static <ReturnType> <MethodName>(<parameters>) => <expression>;
}

<ClassName>.<MethodName>(<arguments>);
PartWhat it does
static class <ClassName>Declares a class that can't be created with new
public static ...Every member must be static too: it belongs to the class, not to an object
<ClassName>.<MethodName>(...)Calls the member through the class's name

Microsoft's static classes guide: "A static class is basically the same as a non-static class, but there's one difference: a static class can't be instantiated. In other words, you can't use the new operator to create a variable of the class type."

The Example​

A Temperature class converts between Celsius and Fahrenheit, and holds the freezing point of water in Celsius as a constant. Nothing about a conversion needs an object, so the class is static.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints Temperature.ToFahrenheit of 20, Temperature.ToCelsius of 68 and Temperature.FreezingPoint, with a static class Temperature holding the constant FreezingPoint and the static methods ToFahrenheit and ToCelsius.

main.cs
Console.WriteLine(Temperature.ToFahrenheit(20));
Console.WriteLine(Temperature.ToCelsius(68));
Console.WriteLine(Temperature.FreezingPoint);

static class Temperature
{
public const double FreezingPoint = 0;

public static double ToFahrenheit(double celsius) => celsius * 9 / 5 + 32;

public static double ToCelsius(double fahrenheit) => (fahrenheit - 32) * 5 / 9;
}

Each method is one line, written after =>, which returns the result of the expression.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
68
20
0

A zsh terminal where dotnet run main.cs prints 68, 20 and 0.

20 °C is 68 °F, and 68 °F is 20 °C. Every call went through the class's name, Temperature., and no object was created anywhere.

The command is the same in zsh, bash and PowerShell.

You Can't Create One​

new on a static class doesn't compile:

main.cs
Temperature converter = new Temperature();

static class Temperature
{
public static double ToFahrenheit(double celsius) => celsius * 9 / 5 + 32;
}
Output
/Users/you/demo/main.cs(1,1): error CS0723: Cannot declare a variable of static type 'Temperature'
/Users/you/demo/main.cs(1,25): error CS0712: Cannot create an instance of the static class 'Temperature'

The build failed. Fix the build errors and run again.

Two errors: even a variable of the type isn't allowed, because there could never be an object to put in it.

Every Member Must Be static​

A member without static doesn't compile either:

main.cs
Console.WriteLine(Temperature.ToFahrenheit(20));

static class Temperature
{
public double Offset = 32;

public static double ToFahrenheit(double celsius) => celsius * 9 / 5 + 32;
}
Output
/Users/you/demo/main.cs(5,19): error CS0708: 'Temperature.Offset': cannot declare instance members in a static class

The build failed. Fix the build errors and run again.

Common Mistakes​

MistakeWhat happensDo this instead
new Temperature()CS0723 and CS0712Call members through the class: Temperature.ToFahrenheit(20)
Leaving out static on a memberCS0708: ... cannot declare instance members in a static classAdd static, or make the class a normal class
Making a class static when each object needs its own dataNowhere to keep that dataUse a normal class

Troubleshooting​

SymptomLikely causeFix
error CS0712: Cannot create an instance of the static classnew on a static classRemove the new, and call the members on the class
error CS0708: ... cannot declare instance members in a static classA field, property or method without staticMark it static

FAQ​

When should a class be static?​

When it only holds helpers that work on what you pass in and keep nothing of their own between calls, like the converter above.

Can a static class have constants?​

Yes. FreezingPoint above is a const, and you read it through the class: Temperature.FreezingPoint.

Conclusion​

Mark a class static when none of it belongs to an object: every member is static, and you call each one through the class's name. C# stops anyone from creating it with CS0712, and from adding an instance member with CS0708.

Additional Resources​