Static members and instance members
An instance member belongs to an object, while a static member belongs to its type. Use instance state for information that differs between objects, and a static member for behavior or data that belongs to the type itself. Static does not mean constant, immutable or thread-safe.
using System;Bin a = new Bin(3);Bin b = new Bin(8);Console.WriteLine(Bin.MaxQuantity);Console.WriteLine(a.Quantity);Console.WriteLine(b.Quantity);class Bin{ public const int MaxQuantity = 10; public int Quantity { get; } public Bin(int n) { if (n < 0 || n > MaxQuantity) throw new ArgumentOutOfRangeException(nameof(n)); Quantity = n; }}The limit belongs to the type; each object stores its own validated quantity.
Expected output (reasoned, not executed):
1038Select the correct owner
Call an instance method through an object reference and a static method through the type name. A static method has no implicit this and cannot directly access instance fields without an object reference. A static class cannot be instantiated and contains static members (constants are implicitly static). It is useful for related stateless helpers but should not become an unstructured collection of global data.
Shared values and initialization
A const field is a compile-time constant and implicitly static. A static readonly field is initialized at runtime, at its declaration or in a static constructor, and cannot be reassigned after initialization. readonly is shallow when the field stores a reference. Mutable static fields are shared across instances of the type; their updates require coordination under concurrency. Do not rely on exact static initialization timing beyond language guarantees, especially when no explicit static constructor exists.
using System;Console.WriteLine(BoxMath.Total(4, 3));Console.WriteLine(BoxMath.Total(0, 9));static class BoxMath{ public static int Total(int boxes, int perBox) => boxes * perBox;}No object is needed for a calculation on explicit small nonnegative inputs.
Expected output (reasoned, not executed):
120using System;Ticket first = new Ticket();Ticket second = new Ticket();Console.WriteLine(first.Id);Console.WriteLine(second.Id);Console.WriteLine(Ticket.Created);class Ticket{ private static int created; public static int Created => created; public int Id { get; } public Ticket() { created++; Id = created; }}Both constructions update the same static counter. Each object retains its own ID. This is a bounded single-thread demonstration, not a production ID generator.
Expected output (reasoned, not executed):
122Common mistake and correction
Do not put per-user or per-object mutable data in a static field simply to make access easier. Every instance would see that shared state. A static counter also is not durable, distributed or automatically safe under concurrent updates; use an appropriate design for those requirements.
Try it yourself
Create a Shelf with a shared const capacity of 6 and an instance property Quantity. Validate initial values between 0 and 6. Construct shelves with 1 and 5, print both quantities and the shared limit, and verify that constructing one with 7 is rejected.
Next: Inheritance and virtual dispatch. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Static members and instance members — knowledge check
Apply the lesson concepts to the following scenarios.
Where does an instance field store its value?
How should an outside caller access the static MaxQuantity constant?
Can a static method directly read an instance field without an object reference?
After two Ticket constructions in the example, what is Ticket.Created?
Does static make created++ automatically safe across threads?