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
{ get; set; }is a full property in five tokens.- The object initializer sets properties as part of construction.
Describe()is expression-bodied: one expression, no braces.
Keywords and builtins used here
BookDescribeclassgetintnewpublicsetstringvar
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.
Step 1 of 4 in Classes & records, step 18 of 29 in Language basics.