Classes, records and object design
Classes model behaviour; records give value-based data a concise shape.
What you will be able to do
- Keep fields private; expose behaviour through methods and properties.
- Use records for messages, DTOs and immutable snapshots.
- Use init-only properties when a value should be set once.
In plain English
A class has identity and mutable state when you need it. A record gives value equality, concise construction and with-expressions for copying. Encapsulate rules inside the type instead of scattering setters across the application.
public record Course(string Slug, string Title, int Lessons);
public class Progress
{
public int Completed { get; private set; }
public void MarkComplete(int total)
{
Completed = Math.Min(total, Completed + 1);
}
}
var html = new Course("html", "HTML Foundations", 8);
var next = html with { Lessons = 9 };Your turn
MediumProtect a score
Create a Score class with a private setter and a method Add that never exceeds 100.
CSHARP · starter
public class Score
{
public int Value { get; set; }
}Check your understanding before moving on.
Knowledge checkQuestion 1
What makes a record different from a class by default?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.