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.
static class Temperature { public static double ToFahrenheit(double c) => c * 9 / 5 + 32; }
Try it
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.
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
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>.InstancesandCounter<string>.Instancesare separate fields. - Global state: mutable static fields are shared by every thread. Use
lock,InterlockedorLock, 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 abstractmembers (C# 11) instead. constversusstatic readonly: aconstis baked into callers at compile time; astatic readonlyis read at run time.