How to Write a static Class in C#
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
static class <ClassName>
{
public static <ReturnType> <MethodName>(<parameters>) => <expression>;
}
<ClassName>.<MethodName>(<arguments>);
| Part | What 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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
dotnet run main.cs
68
20
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:
Temperature converter = new Temperature();
static class Temperature
{
public static double ToFahrenheit(double celsius) => celsius * 9 / 5 + 32;
}
/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:
Console.WriteLine(Temperature.ToFahrenheit(20));
static class Temperature
{
public double Offset = 32;
public static double ToFahrenheit(double celsius) => celsius * 9 / 5 + 32;
}
/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
| Mistake | What happens | Do this instead |
|---|---|---|
new Temperature() | CS0723 and CS0712 | Call members through the class: Temperature.ToFahrenheit(20) |
Leaving out static on a member | CS0708: ... cannot declare instance members in a static class | Add static, or make the class a normal class |
| Making a class static when each object needs its own data | Nowhere to keep that data | Use a normal class |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error CS0712: Cannot create an instance of the static class | new on a static class | Remove the new, and call the members on the class |
error CS0708: ... cannot declare instance members in a static class | A field, property or method without static | Mark 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
- Static classes and static class members, on Microsoft Learn.
- The static modifier, on Microsoft Learn.
