Skip to content

Static fields methods properties

Context: Static members are shared across all instances of a class. They are accessed using the class name, not an instance.

public class Counter
{
public static int Count = 0;
public Counter() => Count++;
}
Counter c1 = new Counter();
Counter c2 = new Counter();
Console.WriteLine(Counter.Count); // 2
public class MathHelper
{
public static int Square(int x) => x * x;
}
int result = MathHelper.Square(5);
public class AppConfig
{
public static string Environment { get; set; } = "Development";
}

Called once before any static member is accessed.

public class Database
{
static Database() => Console.WriteLine("Initialized");
}