Classes, objects & access modifiers
A class defines a reference type that groups state and behavior. An object is a runtime instance of that class. Fields usually hold implementation state; properties expose controlled access; methods implement behavior; constructors establish valid initial state.
Class example
public class TestRun
{
private int _failed;
public string Name { get; }
public int Failed => _failed;
public TestRun(string name)
{
Name = name;
}
public void AddFailure() => _failed++;
}
var run = new TestRun("Smoke");
run.AddFailure();Access modifiers
public exposes a member broadly. private keeps it inside the declaring type. protected makes it available to the type and derived types. internal limits access to the same assembly. Good encapsulation normally keeps implementation details private and exposes the smallest useful public API.
static members belong to the type itself rather than an object instance. Static does not automatically mean singleton; it only changes ownership and access semantics.
Key points
- Class = type definition; object = runtime instance.
- Constructors establish initial object state and can be overloaded.
- Properties are usually preferable to public mutable fields when state is part of a public API.
staticmembers belong to the type rather than a particular instance.