C# static
last modified October 18, 2026
In this article we cover the static modifier keyword in C#.
The static modifier
A static member belongs to the type rather than to a specific object. There is exactly one copy of it, no matter how many instances of the type exist, and it is accessed through the type name.
Console.WriteLine(Math.Abs(-5));
Here, Abs is a static method of the static Math class.
We refer to the method by the type name followed by the dot operator and the
member name.
In classes, structs, and interfaces, we can use the static modifier
for fields, methods, properties, operators, events, and constructors. Local
functions and anonymous functions can be static as well.
A static method has no this reference, so it cannot access instance
members through the implicit receiver. It can still use instance members of any
object it obtains explicitly, for example through a parameter.
The following are features of a static class:
- contains only static members
- cannot be instantiated
- is sealed; other classes cannot inherit from it
- cannot contain instance constructors
The following are features of a static constructor:
- a class or struct can have only one static constructor
- it has no access modifier and no parameters
- it cannot be called directly; the runtime calls it automatically before the first instance is created or any static member is accessed
- it is used to initialize static state that cannot be set by a field initializer
- if the type has no static constructor, static fields get their default values or their initializer values
C# static member
A static member belongs to the type.
Console.WriteLine(MyMath.PI);
Console.WriteLine(MyMath.Square(7));
Console.WriteLine(Math.Abs(-5));
static class MyMath
{
public const double PI = 3.14159265358979323846;
public static int Square(int x) => x * x;
}
We have a static MyMath class with a constant and a method. The
class is declared at the end of the file, after the top-level statements.
Console.WriteLine(MyMath.PI); Console.WriteLine(MyMath.Square(7)); Console.WriteLine(Math.Abs(-5));
We refer to the members of our own MyMath class and to the
Abs method of the built-in Math class. A const
field is implicitly static, so we write const, not
static const.
$ dotnet run 3.141592653589793 49 5
C# static field shared by instances
A static field in a non-static class is shared by all instances. A typical use is counting the created objects.
var a = new Counter();
var b = new Counter();
var c = new Counter();
Console.WriteLine($"{a.Id} {b.Id} {c.Id}");
Console.WriteLine($"created: {Counter.Count}");
class Counter
{
public static int Count { get; private set; }
public int Id { get; }
public Counter()
{
Count++;
Id = Count;
}
}
Each Counter instance has its own Id, but there is one
Count property for the whole type.
public static int Count { get; private set; }
Any code can read the count through Counter.Count, but only the class
itself can change it. Static state shared across threads must be synchronized; the
Count++ above is not thread-safe.
$ dotnet run 1 2 3 created: 3
C# static method
A static method is called on the type, not on an instance. Inside it we can use static members directly. Instance members can be reached only through an object reference.
Console.WriteLine(Helper.Twice(4));
var user = new User("Alice");
user.Greet();
Console.WriteLine(Counter.Total);
class Counter
{
public static int Total = 10;
public int Local = 1;
// a static method cannot use Local directly, but it can use an instance it receives
public static int Sum(Counter c) => Total + c.Local;
}
class User(string name)
{
public void Greet() => Console.WriteLine($"Hello {name}, total is {Counter.Total}");
}
static class Helper
{
public static int Twice(int n) => n * 2;
}
We have static methods in several classes.
public static int Sum(Counter c) => Total + c.Local;
The Sum method reads the static Total field directly and
the instance Local field through the parameter c. Writing
plain Local here would not compile.
Console.WriteLine(Helper.Twice(4));
Static methods suit pure helpers that do not depend on object state.
$ dotnet run 8 Hello Alice, total is 10 10
C# static constructor
A static constructor initializes the static state of a type. It runs once, before the type is first used.
Console.WriteLine("Main starts");
Console.WriteLine(Config.Created);
class Config
{
public static readonly DateTime Created;
public static readonly string[] Names;
static Config()
{
Console.WriteLine("static constructor runs");
Created = new DateTime(2026, 10, 18);
Names = ["alpha", "beta"];
}
}
The output shows when the static constructor runs.
Console.WriteLine("Main starts");
Console.WriteLine(Config.Created);
The first access to Config.Created triggers the static constructor.
$ dotnet run Main starts static constructor runs 10/18/2026 12:00:00 AM
The date format depends on the current culture. A static readonly
field can be assigned only in a field initializer or in the static constructor.
Unlike const, it is evaluated at run time and can hold any object.
C# extension methods
The static keyword is used to define extension methods. Extension
methods are added to existing types without creating a derived type, recompiling,
or otherwise modifying the original type.
var vals = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
vals.Shuffle();
Console.WriteLine(string.Join(" ", vals));
Console.WriteLine("hello".Repeat(3));
static class MyExtensions
{
public static void Shuffle<T>(this IList<T> vals)
{
for (int n = vals.Count - 1; n > 0; n--)
{
int k = Random.Shared.Next(n + 1);
(vals[k], vals[n]) = (vals[n], vals[k]);
}
}
public static string Repeat(this string s, int count) =>
string.Concat(Enumerable.Repeat(s, count));
}
In the example, we define the Shuffle and Repeat
extension methods.
vals.Shuffle();
We call Shuffle as if it were a method of the list. The compiler
translates it to MyExtensions.Shuffle(vals).
public static void Shuffle<T>(this IList<T> vals)
An extension method is a static method in a static, non-generic, non-nested class.
Its first parameter is preceded by the this modifier and names the
type being extended.
int k = Random.Shared.Next(n + 1); (vals[k], vals[n]) = (vals[n], vals[k]);
This is the Fisher-Yates shuffle. We use the shared random generator and a tuple swap.
$ dotnet run 6 3 5 2 1 4 8 7 hellohellohello
The order of the shuffled list differs on each run.
C# 14 extension members
C# 14 (.NET 10) adds extension blocks. Inside a static class, an
extension block names the receiver once and can declare extension
methods, extension properties, and even static extension members.
Console.WriteLine("hello".Repeat(3));
Console.WriteLine("level".IsPalindrome);
Console.WriteLine("falcon".IsPalindrome);
Console.WriteLine(string.FromChars('a', 'b', 'c'));
static class StringExtensions
{
extension(string s)
{
public string Repeat(int count) => string.Concat(Enumerable.Repeat(s, count));
public bool IsPalindrome => s.SequenceEqual(s.Reverse());
}
extension(string)
{
public static string FromChars(params char[] chars) => new(chars);
}
}
We extend the string type with a method, a property, and a static
method.
extension(string s)
{
public string Repeat(int count) => string.Concat(Enumerable.Repeat(s, count));
public bool IsPalindrome => s.SequenceEqual(s.Reverse());
}
The receiver parameter s is available to all members of the block.
Members are declared without the static modifier and without the
this parameter. IsPalindrome is an extension property,
which the classic syntax could not express.
extension(string)
{
public static string FromChars(params char[] chars) => new(chars);
}
A receiver without a name declares static extension members, which are called on
the type itself: string.FromChars(...).
$ dotnet run hellohellohello True False abc
This example requires the .NET 10 SDK; the classic syntax still works everywhere.
C# using static directive
The using static directive allows us to access static members and
nested types of a type without qualifying them with the type name.
using static System.Math; using static System.Console; WriteLine(Sqrt(3 * 3 + 4 * 4)); WriteLine(Max(3, 9)); WriteLine(PI);
In the example, we import the members of System.Math and
System.Console.
$ dotnet run 5 9 3.141592653589793
C# static anonymous functions and local functions
A static lambda or local function cannot capture variables from the
enclosing scope, nor this. This avoids accidental captures, which
allocate a closure object.
int factor = 3; // a static lambda cannot capture local variables Func<int, int> triple = static x => x * 3; Console.WriteLine(triple(5)); Console.WriteLine(Scale(5)); int Scale(int x) => Helper(x, factor); // a static local function cannot capture either; everything is passed in static int Helper(int x, int f) => x * f;
The lambda triple uses only its parameter. The static local function
Helper receives everything it needs as arguments. If either tried to
use factor, the compiler would report an error.
$ dotnet run 15 15
C# static abstract interface members
Since C# 11, interfaces can declare static abstract members, which
implementing types must provide. Combined with generics, this lets us call static
members through a type parameter. The generic math types in .NET are built on it.
Console.WriteLine(Describe<Cat>());
Console.WriteLine(Describe<Dog>());
static string Describe<T>() where T : IAnimal => $"{T.Name} says {T.Sound}";
interface IAnimal
{
static abstract string Name { get; }
static abstract string Sound { get; }
}
class Cat : IAnimal
{
public static string Name => "cat";
public static string Sound => "meow";
}
class Dog : IAnimal
{
public static string Name => "dog";
public static string Sound => "woof";
}
The IAnimal interface requires every implementor to have a static
Name and Sound.
static string Describe<T>() where T : IAnimal => $"{T.Name} says {T.Sound}";
We access the static members directly through the type parameter T;
no instance is needed.
$ dotnet run cat says meow dog says woof
Source
static modifier - language reference
In this article we have worked with the static modifier in C#.
Author
List all C# tutorials.