Classes & Objects
Classes define blueprints; objects are runtime instances with fields, properties, and methods.
Introduction
Classes define blueprints; objects are runtime instances with fields, properties, and methods. C# is single-inheritance for classes but supports multiple interface implementation — the OOP model behind every ASP.NET Core controller, EF entity, and WPF ViewModel.
Modern C# adds primary constructors (C# 12), required properties, and init accessors for immutable object graphs. Interviewers expect you to explain stack vs heap allocation, object identity, and when to use class vs struct vs record.
This lesson builds a domain model you will extend through encapsulation, inheritance, and polymorphism in the following modules.
The story
An e-commerce platform stores customer profiles with a stable ID, a verified email, and a legal name required for invoicing. Support agents can update an email address through a controlled method that trims whitespace and lowercases the domain — but they cannot assign an empty string or bypass validation by setting a public field directly.
Classes bundle data and behavior; objects are the live instances created when someone registers on the website.
Understanding the topic
Key concepts
- class keyword defines reference type on heap.
- new allocates object and calls constructor.
- Fields store state; properties control access with get/set.
- this refers to current instance.
- static members belong to type, not instance.
- records are reference types with value-based equality by default.
classDiagramclass BankAccount {-decimal balance+Deposit(amount)+Withdraw(amount)+GetBalance()}BankAccount --> Transaction : uses
Step-by-step explanation
- Declare class with members; instantiate with new ClassName(args).
- GC reclaims unreachable objects — no manual free.
- Reference variables alias same object when assigned.
- Object initializer syntax: new Customer { Name = "A" }.
- Primary constructor parameters can initialize fields/properties.
- sealed prevents inheritance; abstract requires subclass.
Practical code example
Customer entity with primary constructor, required property, and domain method:
namespace TechLearningPro.Classes;public sealed class Customer(string id, string email){public string Id { get; } = id;public required string LegalName { get; init; }public string Email { get; private set; } = email;public void UpdateEmail(string newEmail){ArgumentException.ThrowIfNullOrWhiteSpace(newEmail);Email = newEmail.Trim().ToLowerInvariant();}}
Line-by-line code explanation
public sealed class Customer(string id, string email)uses a primary constructor (C# 12) to capture constructor parameters.public string Id { get; }exposes a read-only identifier set once at construction.public required string LegalName { get; init; }must be supplied during object initialization.public string Email { get; private set; } = emailallows internal updates but not public assignment.UpdateEmail(string newEmail)is the only supported way to change the email address.ArgumentException.ThrowIfNullOrWhiteSpace(newEmail)rejects null, empty, or whitespace-only input.Email = newEmail.Trim().ToLowerInvariant()normalizes the address before storing it.sealedprevents further inheritance — appropriate when the class is a concrete domain entity.
Key takeaway: Primary constructor (C# 12) reduces boilerplate. required ensures object initializer provides LegalName. Encapsulate mutation in methods.
Real-world use
Where you'll use this in production
- Domain entities in DDD bounded contexts.
- DTOs and ViewModels in MVC/MVVM apps.
- Plugin classes loaded via reflection in extensible platforms.
- Game entities with Update loop methods.
Best practices
- Favor immutability with init or record types.
- Keep fields private; expose properties.
- Single responsibility per class.
- Use factory methods for complex construction.
- Override ToString for debugging; not for production logs of PII.
Common mistakes
- Public mutable fields breaking encapsulation.
- God classes with 50+ members.
- Confusing class (reference) with struct (value) semantics.
- Creating objects in tight loops without pooling consideration.
Advanced interview questions
Q1BeginnerClass vs object?
Q2BeginnerWhat is static?
Q3IntermediateClass vs struct?
Q4IntermediatePurpose of sealed?
Q5AdvancedModel immutable Order with line items in C# 12.
Summary
Classes define types; objects are instances with identity on the heap. Properties encapsulate state; methods encapsulate behavior. Primary constructors and required/init modernize object creation. Choose class vs struct vs record based on semantics and size. Next: constructors and object initialization.