C# Reference ยท Classes and Object-Oriented Concepts
Static members
Reference for C# static members: static fields, methods, properties, static classes, static constructors, static abstract interface members and thread-safety notes.
A static member belongs to the type itself instead of to any one object. There is exactly one copy of a static field no matter how many instances exist, and you reach static members through the type name: Math.Max(1, 2), Counter.Total.
Types can have static fields, properties, methods, events, operators and constructors. A whole class can be static: it then contains only static members, cannot be instantiated and cannot be inherited. Utility groups such as Math, Console and File are static classes.
public class Counter { public static int Total; // shared by all instances public Counter() => Total++; public static void Reset() => Total = 0; }
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
public static int Count; // static field public static string Name { get; set; } = ""; // static property public static void Log(string text) { } // static method static MyType() { } // static constructor public static MyType operator +(MyType a, MyType b) => ...; public static class Helpers { } // static class
| Part | Description |
|---|---|
| Access | Through the type name. Inside the type you may use the bare name. this does not exist in static members. |
| Static class | Sealed and abstract at the same time; only static members (and const). |
| Static constructor | No parameters, no access modifier, runs once per type before first use. |
const |
Implicitly static, but its value is inlined at compile time. |
static readonly |
A runtime-initialized shared value, set in an initializer or the static constructor. |
Usage notes
- A static method cannot touch instance members directly, because it has no instance. Pass the object in as an argument (as
Describe(Ticket t)does). - Static state is global state. It lives as long as the application domain, is shared by all threads and makes tests harder because it leaks between them. Prefer instances and dependency injection for anything that is not a constant or a pure helper.
- Thread safety is your job.
_next++above is not atomic; with several threads useInterlocked.Increment(ref _next)or a lock. constversusstatic readonly:constvalues are copied into every assembly that uses them, so changing one requires recompiling dependents. Usestatic readonlyfor values that may change between versions.- Static constructors run lazily, once. If one throws, the type is unusable and callers get
TypeInitializationException. Prefer field initializers when possible. - Static fields in generic types are per closed type:
Cache<int>.ValueandCache<string>.Valueare different variables. - Static abstract and virtual members in interfaces (C# 11) enable generic math (
T.Zero,T.Parse) and "static polymorphism" through type parameters. - Extension methods must be declared as static methods of a non-generic static class.
using static System.Math;imports static members so you can writeSqrt(x)without the type name.