Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The static keyword

Reference for the C# static keyword: static fields, methods, classes, constructors, local functions and lambdas, with initialization order and pitfalls.

static means "belongs to the type itself, not to any instance". A static field exists once per type (per closed generic type), a static method is called on the type name (Math.Max), and no this reference is available inside it. Static members are the right home for utilities, constants-like data, factory methods and counters shared by all instances.

You can also mark a whole class static: it may then contain only static members and can neither be instantiated nor inherited. Static also appears in using static imports, static constructors, static local functions and static lambdas.

C#
static class Temperature
{
    public static double ToFahrenheit(double c) => c * 9 / 5 + 32;
}

Try it

C# Static fields, methods and constructors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Static classes, local functions and lambdas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
static class Name { ... }                 // only static members

class C
{
    public static int Field;
    public static int Prop { get; set; }
    public static void Method() { }
    static C() { }                        // static constructor: no access modifier, no parameters
    public static C operator +(C a, C b) => ...;   // operators must be static
}

static (int x) => x;                       // static lambda (C# 9)
static int Local(int n) => n;              // static local function (C# 8)
using static System.Math;                  // import static members

Errors: calling a static member through an instance is CS0176; declaring an instance member in a static class is CS0708; instantiating a static class is CS0723.

Usage notes

  • Initialization order: static field initializers run in textual order, then the static constructor, before the first access to a static member or the first instance. Without a static constructor the runtime may initialize earlier (beforefieldinit).
  • Static constructor failures: an exception makes the type unusable. Every later access throws TypeInitializationException.
  • Per closed generic type: Counter<int>.Instances and Counter<string>.Instances are separate fields.
  • Global state: mutable static fields are shared by every thread. Use lock, Interlocked or Lock, or avoid them. They also make tests harder.
  • Static classes are sealed, abstract in metadata, and the usual place for extension methods and helper methods.
  • Cannot be virtual: static methods are not overridden. Interfaces can declare static abstract members (C# 11) instead.
  • const versus static readonly: a const is baked into callers at compile time; a static readonly is read at run time.