Edit

C# classes

Tip

New to developing software? Start with the Get started tutorials first.

A class defines a type that groups data and behavior. Use a class to represent something your program works with, such as a counter, a document, or a connection. An object, also called an instance, is a value of that type. The class reference describes the declaration syntax.

Classes are reference types: a variable holds a reference to an object rather than the object's data. Multiple variables can refer to the same object. See Value types and reference types for the type-system vocabulary.

For example, VisitCounter defines the state and operations for a counter:

public class VisitCounter
{
    private int count;
    private static int total;

    public void AddVisit()
    {
        count++;
        total++;
    }

    public int GetCount() => count;

    public static int GetTotal() => total;

    public static int GetTotalExcluding(VisitCounter counter) =>
        total - counter.count;
}

GetCount and GetTotal are expression-bodied methods: each => expression; is shorthand for { return expression; }.

The declarations inside a class are its members. Here, fields store counts and methods provide operations. private limits access to the class, and public lets other code use a member. For the next level of detail, see Class members and instances.

Create objects

The class declaration defines the type; it doesn't create an object. The new operator creates an instance:

Console.WriteLine(VisitCounter.GetTotal()); // => 0
var first = new VisitCounter();
var second = new VisitCounter();

first.AddVisit();
Console.WriteLine(first.GetCount()); // => 1
Console.WriteLine(second.GetCount()); // => 0
Console.WriteLine(VisitCounter.GetTotal()); // => 1

second.AddVisit();
second.AddVisit();
Console.WriteLine(first.GetCount()); // => 1
Console.WriteLine(second.GetCount()); // => 2
Console.WriteLine(VisitCounter.GetTotal()); // => 3

Each object has its own instance state. The static field total belongs to the type and holds state shared by both counters. Access the static method through VisitCounter, not through first or second.

Assignment shares a reference

A class variable holds a reference to an object. Assignment copies that reference:

var alias = first;
alias.AddVisit();
Console.WriteLine(first.GetCount()); // => 2
Console.WriteLine(ReferenceEquals(first, alias)); // => True
Console.WriteLine(first == second); // => False

first and alias refer to the same object. A change through either variable affects that object. second refers to a different object even though its count is also 2.

For an ordinary class that doesn't customize equality, == compares whether the references identify the same object. ReferenceEquals tests that identity explicitly. See Equality comparisons for more detail.

Record classes

For a class whose main purpose is to store data, use a record class when you want the compiler to provide value equality and copy support. record is shorthand for record class. Record classes are still reference types, so assignment still copies a reference.

Continue with C# record types for positional declarations, generated members, and shallow with copies.

Next steps