Menu

C# static: Static Classes, Methods, Fields and Constructors

What static means in C#: members that belong to the type instead of an object, shared state across instances, static methods and static classes, static constructors, const vs static readonly, using static, and the CS0120 error.

This page includes runnable editors - edit, run, and see output instantly.

static marks a member that belongs to the type, not to an object. An instance field exists once per object; a static field exists once, full stop. An instance method runs on an object (order.Ship()); a static method runs on the type (Math.Round(x)) and needs no object at all.

Static fields: one copy, shared

The clearest way to see the difference is a counter. Each Order has its own Id, but the count of orders created so far has to live somewhere shared.

Output:

Ana #1, Ben #2, Chen #3
Orders created: 3

Static members are accessed through the type name: Order.Created, not a.Created. C# does not allow reaching a static member through an instance (error CS0176), which makes it obvious at the call site that the value is shared.

Static methods

A static method has no this. It works with its parameters and with other static members only. That makes it the natural shape for helpers that compute something from their inputs:

Output:

61.38 EUR
7

Main itself is static: the runtime calls it before any object exists.

The CS0120 error: static code using instance members

Because Main is static, beginners often hit this error as soon as they add a second method:

class Program
{
    int visits;
    void Greet() { Console.WriteLine("Hello"); }

    static void Main()
    {
        Greet();    // error CS0120: An object reference is required for the non-static field, method, or property 'Program.Greet()'
        visits++;   // error CS0120: An object reference is required for the non-static field, method, or property 'Program.visits'
    }
}

A static method has no object, so there is no visits for it to increment. Two fixes, and the right one depends on what the member is:

  • The member really is per-object data: create an object and use it. var app = new Program(); app.Greet();
  • The member does not need object state: mark it static. static void Greet() { ... }

Marking everything static to make the error go away works in a tiny program and turns into global state in a real one. Decide by asking whether the data belongs to one object or to the whole program.

The reverse direction is always allowed: instance methods can call static members freely.

Static classes

A class declared static may contain only static members and can never be instantiated. It is a named group of functions and constants.

Output:

77
100
True

The compiler enforces the contract: an instance member inside a static class is error CS0708, and new on it is error CS0712. Math, Console, File, Path and Enumerable are all static classes. Extension methods must also live in a static class.

Static constructors and static readonly

A static field whose value needs more than one expression can be set in a static constructor, which the runtime runs once, before the type is first used:

Output:

Before first use
ShippingRates initialized
5.50
3
2026-01-01

readonly means the field can be assigned only in its initializer or the static constructor. After that it cannot be reassigned, though the object it points to could still be mutable, which is why the dictionary is exposed as IReadOnlyDictionary.

const vs static readonly

Both give you a named value shared by the whole program, and they differ in when the value is fixed:

conststatic readonly
Value fixed atcompile timerun time, once
Allowed typesnumbers, char, bool, string, enums, nullany type
Implicitly staticyes (cannot write static const)declared static explicitly
Copied into calling assembliesyesno, read from the field
Usable in switch cases and attribute argumentsyesno

The copying row matters for libraries: if assembly A declares public const int MaxItems = 50; and assembly B uses it, B contains the literal 50. Changing A to 100 does nothing for B until B is recompiled. For values that might change between versions, prefer static readonly.

using static

A using static directive (C# 6) imports the static members of a type, so you can call them without the type name:

Output:

Area: 28.27
40

It reads well for math-heavy code. Use it sparingly elsewhere: a bare WriteLine or Parse hides which type the method comes from.

Static state and threads

A static field is shared by every thread in the process. Incrementing one from several threads at once loses updates, because created++ is a read, an add and a write, and two threads can interleave them. Use Interlocked.Increment(ref created) for counters, or a lock for anything larger. Web applications are the usual place this bites: every request runs on some thread, so a static field holding "the current user" is shared by all users.

One more subtlety: in a generic class, each constructed type gets its own static fields. Cache<int>.Count and Cache<string>.Count are two different fields.

When to use static

Good uses:

  • Pure helpers that compute a result from their arguments: formatting, conversions, validation.
  • Constants and read-only lookup data shared by the program.
  • Factory methods, such as TimeSpan.FromMinutes(5) or your own Temperature.FromCelsius(21).
  • Extension methods.

Poor uses:

  • Mutable global state (the current user, a shopping cart, settings that change). It couples every piece of code that touches it and makes tests depend on each other's leftovers.
  • Anything you will want to swap in tests, such as a clock, a database or an email sender. Static calls cannot be replaced by a fake; an instance passed to the constructor can.

Frequently Asked Questions

What does static mean in C#?

A static member belongs to the type itself rather than to any object. There is one copy of a static field no matter how many objects exist, and a static method is called on the type name (Math.Max(3, 7)) without creating an instance. A static method has no this, so it cannot use instance fields directly.

What is a static class in C#?

A class declared static can contain only static members and can never be instantiated (new on it is error CS0712). It groups related functions that need no object state, like Math, Console or File. Extension methods must be declared in a static class.

How do I fix "An object reference is required for the non-static field, method, or property"?

That is error CS0120: a static method, often Main, is using an instance member. Either create an object and call the member on it (var app = new Program(); app.Run();), or mark the member static if it does not need per-object data.

What is the difference between const and static readonly in C#?

A const is a compile-time constant (numbers, strings, bool, enums) whose value is copied into every assembly that uses it. A static readonly field is assigned once at run time, in its initializer or the static constructor, and can hold any type, such as a DateTime or a list. Use const for values that will never change, static readonly for everything else.

When should I use static in C#?

Use static for code that works only on its parameters (pure helpers such as a price formatter), for constants and caches that are truly global to the program, and for factory methods. Avoid static for anything that holds per-user or per-request state, or that you will want to replace in tests: shared mutable state is hard to test and unsafe across threads without locking.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED