Skip to main content

How to Write a static Method in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Some methods belong to a class but need no object of it: checking whether a quantity is allowed, building a value from what you pass in. In C#, mark such a method static and call it on the class name. Methods without static belong to one object and work with its data.

Syntax​

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

<ClassName>.<MethodName>(<arguments>);
PartWhat it does
staticMakes the method belong to the class itself, not to an object
<ClassName>.<MethodName>(...)Calls it through the class's name, without creating an object
What it can useIts parameters and other static members, but not the fields and properties of an object

Microsoft's static modifier reference: "Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object." Its static members guide adds: "The static member is always accessed by the class name, not the instance name."

The Example​

An Order has an item and a quantity. Its static method IsValidQuantity checks whether a quantity is allowed, which needs no order at all, while its ordinary method Describe describes one order.

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 Order.IsValidQuantity of 3 and of 0, then creates an Order for 3 Tea and prints order.Describe(), with an Order class that has Item and Quantity properties, a static IsValidQuantity method that checks for 1 to 10, and an instance Describe method.

main.cs
Console.WriteLine(Order.IsValidQuantity(3));
Console.WriteLine(Order.IsValidQuantity(0));

Order order = new() { Item = "Tea", Quantity = 3 };
Console.WriteLine(order.Describe());

class Order
{
public string Item { get; set; } = "";
public int Quantity { get; set; }

public static bool IsValidQuantity(int quantity) => quantity >= 1 && quantity <= 10;

public string Describe() => $"{Quantity} x {Item}";
}

Step 2: Run It​

Terminal
dotnet run main.cs
Output
True
False
3 x Tea

A zsh terminal where dotnet run main.cs prints True, False and 3 x Tea.

  1. Order.IsValidQuantity(3) and (0) ran before any order existed: a static method needs none.
  2. order.Describe() was called on one order, and read that order's Quantity and Item.

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

A static Method Can't See an Object's Data​

A static method has no order to read from, so it can't use Quantity:

main.cs
Console.WriteLine(Order.IsValid());

class Order
{
public int Quantity { get; set; }

public static bool IsValid() => Quantity >= 1;
}
Output
/Users/you/demo/main.cs(7,37): error CS0120: An object reference is required for the non-static field, method, or property 'Order.Quantity'

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

The guide: "Static methods and properties can't access non-static fields and events in their containing type, and they can't access an instance variable of any object unless it's explicitly passed in a method parameter." Pass the value in as a parameter, as IsValidQuantity(int quantity) does.

Call It on the Class, Not on an Object​

Calling a static method through an object doesn't compile either:

main.cs
Order order = new();
Console.WriteLine(order.IsValidQuantity(3));

class Order
{
public static bool IsValidQuantity(int quantity) => quantity >= 1 && quantity <= 10;
}
Output
/Users/you/demo/main.cs(2,19): error CS0176: Member 'Order.IsValidQuantity(int)' cannot be accessed with an instance reference; qualify it with a type name instead

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

Write Order.IsValidQuantity(3).

Common Mistakes​

MistakeWhat happensDo this instead
Reading a property of the object inside a static methoderror CS0120: An object reference is required for the non-static field, method, or propertyPass the value as a parameter, or drop static
Calling a static method through an objecterror CS0176: ... cannot be accessed with an instance reference; qualify it with a type name insteadCall it on the class name
Making a method static that needs an object's dataIt can't reach the dataKeep it an instance method

Troubleshooting​

SymptomLikely causeFix
CS0120 inside a methodThe method is static but uses instance dataPass the data in, or remove static
CS0176 on a callobject.Method() for a static methodClassName.Method()

FAQ​

When should a method be static?​

When it works only with what you pass in, like IsValidQuantity, and doesn't need an object's fields or properties. If it needs them, like Describe, it's an instance method.

Can a normal class have static methods?​

Yes, as Order does: a class can mix static and instance members. A class marked static can hold only static ones.

Conclusion​

A static method belongs to the class: call it as ClassName.Method(), with no object, and give it what it needs as parameters. It can't read an object's data, CS0120, and it can't be called through an object, CS0176.

Additional Resources​