Class (C#)

A class is a blueprint for creating objects that bundle together data (fields and properties) and behavior (methods). You define one with the class keyword.

C#

Each object made from a class is called an instance, created with the new keyword. Classes are the foundation of object-oriented programming in C#.

Members marked public are accessible from outside the class, while private members are hidden and used only internally.

Syntax

class Name { ... }

Examples

C#

class Dog
{
    public string Name;
    public string Speak() => Name + " says woof";
}

Dog d = new Dog { Name = "Rex" };
Console.WriteLine(d.Speak());

Output:

Rex says woof

Learn it in a lesson

See also

Back to the Dictionary