×

c# · level 2 · unit 4 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a data class written in one line.

Lots of classes only carry data: a book's title and price, a point on a map. A record writes all of that in one line. record Book(string Title, decimal Price); builds the constructor, a property for each part, and a ToString that shows them all.

Records use : two records with the same contents are equal, so == gives True. Two class objects are only equal when they are the very same object, even if every field matches.

A record's properties are set once, when it is built. That makes it , so a.Price = 5; is refused with CS8852.

using System;

var a = new Book("Holes", 6.99m);
var b = new Book("Holes", 6.99m);
Console.WriteLine(a);
Console.WriteLine(a == b);
Console.WriteLine($"{a.Title} costs {a.Price}");

record Book(string Title, decimal Price);

Record parts are properties, so they start with a capital letter.