Learn C# in Y Minutes
C# 14 · .NET 10

05. Classes

Primitive types hold one simple value. Real programs deal with bigger things: a customer, an order, a product. A class lets you create your own type that bundles related data and behavior together.

Class vs object

A class is a blueprint. It describes what every object of that kind has and can do, but it is not a thing itself. An object (or instance) is a real thing built from that blueprint, living in memory. One class, as many objects as you need.

One blueprint labeled class Person with arrows to three slightly different houses labeled object
One class, many objects built from it.

Fields and new

Declare a class with the class keyword. Variables inside it are fields: the data each object carries. Create an object with new, then use a dot to reach its members. Each object has its own copy of the fields, so Ada's age does not affect Alan's.

app/05-classes/Person.cs
var ada = new Person();
ada.Name = "Ada";
ada.Age = 36;

var alan = new Person();
alan.Name = "Alan";
alan.Age = 41;

ada.Introduce();
alan.Introduce();

class Person
{
    public string Name = "";
    public int Age;

    public void Introduce()
    {
        Console.WriteLine($"Hi, I am {Name} and I am {Age}.");
    }
}
Hi, I am Ada and I am 36.
Hi, I am Alan and I am 41.

public means code outside the class can use the member. Without it, members are private. Access modifiers get a full lesson in the intermediate course.

Methods

A method is a named block of code that belongs to a class. It can take parameters (inputs) and return a value. The type before the name is what it returns; void means it returns nothing. A one-line method can use the => shorthand.

app/05-classes/Methods.cs
var calculator = new Calculator();

int sum = calculator.Add(2, 3);
Console.WriteLine($"2 + 3 = {sum}");
Console.WriteLine($"Is 7 even? {calculator.IsEven(7)}");
calculator.PrintSquare(4);

class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public bool IsEven(int number) => number % 2 == 0;

    public void PrintSquare(int number)
    {
        Console.WriteLine($"{number} squared is {number * number}");
    }
}
2 + 3 = 5
Is 7 even? False
4 squared is 16

Static members

Normal members belong to each object. A static member belongs to the class itself, shared by everyone, and you call it on the class name. That is why you write Console.WriteLine(...) and Math.Max(...) without ever creating a Console or Math object.

app/05-classes/Static.cs
var grace = Person.Parse("Grace");
var linus = Person.Parse("Linus");

grace.Introduce();
linus.Introduce();
Console.WriteLine($"People created: {Person.Count}");

Console.WriteLine($"Math.Max(3, 7) = {Math.Max(3, 7)}");

class Person
{
    public static int Count;

    public string Name = "";

    public static Person Parse(string name)
    {
        var person = new Person();
        person.Name = name.Trim();
        Count++;
        return person;
    }

    public void Introduce()
    {
        Console.WriteLine($"Hi, I am {Name}.");
    }
}
Hi, I am Grace.
Hi, I am Linus.
People created: 2
Math.Max(3, 7) = 7

Person.Parse is a common pattern: a static method that builds and returns a new object. You have already used one: int.Parse.

A static method cannot touch instance fields like Name directly, because there is no "current object". It has to create or receive one first.

Almost everything in a Web API is a class: the data you send and receive (models), the code that handles requests (controllers), and the business logic in between (services).

ProductsController.cs
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public string[] GetAll() => ["Keyboard", "Mouse"];
}

Quick quiz