typestar

Classes & properties in C#

Auto-properties and object initializers, the shape of everyday C#.

var book = new Book { Title = "Dune", Pages = 412 };
book.Pages += 20;
Console.WriteLine(book.Describe());

class Book
{
    public string Title { get; set; } = "";
    public int Pages { get; set; }

    public string Describe() => $"{Title}, {Pages} pages";
}

How it works

  1. { get; set; } is a full property in five tokens.
  2. The object initializer sets properties as part of construction.
  3. Describe() is expression-bodied: one expression, no braces.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
249
Tokens
65
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 43 seconds.

Type this snippet

Step 1 of 4 in Classes & records, step 18 of 29 in Language basics.

← Previous Next →