Static members

A static member belongs to a struct type itself rather than to one particular instance.

Static fields

A static field has one storage location for its declaring type. It is not copied into each struct value and does not contribute to the instance layout.

struct Entity
{
    public static int Count;
    public static int Maximum = 1024;
}

Access static fields through the type name. A field without an initializer is zero-initialized. An explicit initializer uses a restricted compile-time expression made from primitive literals and unary or binary operators.

Entity.Count = Entity.Count + 1;
int limit = Entity.Maximum;

Static methods

A static method has no implicit instance and therefore no implicit this. It may access static members directly and receives any instance data through explicit parameters.

struct Vector3
{
    public float X;
    public float Y;
    public float Z;

    public static Vector3 Zero()
    {
        return Vector3 { 0.0f, 0.0f, 0.0f };
    }

    public static float Sum(Vector3 value)
    {
        return value.X + value.Y + value.Z;
    }
}

Vector3 origin = Vector3.Zero();
float total = Vector3.Sum(origin);

Static, readonly and const

These keywords describe different aspects of a declaration. static selects type-level runtime storage, readonly restricts mutation of that storage, and const declares a compile-time value.

struct Limits
{
    public static int ActiveConnections;
    public static readonly int Capacity = 512;
    const int HeaderSize = 16;
}