Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Programming · C# · Chapter 03 / 10

Programming learning path

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.
  • static members belong to the type rather than a particular instance.

Source registry

2 chapter references
A tour of C#

Core language syntax, control flow, methods, classes and general C# semantics

Microsoft Learn · Official documentation
Source ↗
Object-oriented programming

Encapsulation, inheritance, polymorphism and abstraction

Microsoft Learn · Official documentation
Source ↗