Skip to content
Guest
Learn C# · Course Syllabus Show lessons
Object-Oriented C# · 9 min read · Beginner

Classes, Properties & Records

Model clean domain types with C# auto-properties, init-only setters, and immutable records with non-destructive 'with' copies.

What you'll learn

  • Use C# properties ({ get; init; } and { get; private set; }) instead of manual getters/setters
  • Define value-equal immutable models in one line with record
  • Create modified copies of records cleanly using the with expression

In C#, properties (public decimal Balance { get; private set; }) give you clean field-like access syntax while keeping full encapsulation. For immutable data models, record adds built-in value equality and non-destructive copying via with.

Fill-in-the-Blank Exercise

Record & With Expression

Declare a Course record and create a copy with Lessons updated to 8.

public  Course(string Title, int Lessons);

var v1 = new Course("C#", 6);
var v2 = v1  { Lessons = 8 };

Interactive Code Lab

C#RecordsAndProps.cs
Expected OutputClick “Run Live” to compile with Mono C#
Learner { Name = Sam, Completed = 4 }
Learner { Name = Sam, Completed = 6 }
Same record? False

Check Your Understanding

Score: 0 / 1

1. What does { get; private set; } on a C# property allow?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind