Tutorials C# & .NET 8 Architect Mastery

Required Members & Init-Only properties

On this page

Safety with 'Required' & 'Init'

In traditional C#, you often ended up with "Null Reference Exceptions" because someone forgot to set a property. Required Members and Init-Only Props solve this at the compiler level.

1. Required Members (C# 11)

By adding the `required` keyword, you FORCE the developer to set that property when creating the object.

public class User {
    public required string Email { get; set; }
}
var u = new User(); // COMPILER ERROR: You must set Email!

2. Init-Only Properties

The `init` keyword allows you to set a property during creation, but NEVER change it again (making it immutable).

public record Product {
    public string Name { get; init; }
}
product.Name = "New Name"; // COMPILER ERROR: It's readonly!

4. Interview Mastery

Q: "How do 'Required' members help with Domain Driven Design (DDD)?"

Architect Answer: "In DDD, an object should never exist in an 'Invalid State.' Before `required`, we had to use complex constructors to ensure data validity. Now, we can use clean object initializers while still guaranteeing that critical fields (like IDs or Keys) are present. It combines the flexibility of properties with the safety of constructors."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

C# & .NET 8 Architect Mastery
Course syllabus
1. Memory Management & Performance
2. Advanced Asynchronous Programming
3. Modern C# 12+ Features
4. Enterprise Design Patterns in .NET
5. Dynamic Programming & Reflection
6. Testing & Quality Architecture
7. Modern Web API Architectures
8. FAANG .NET Architect Interview
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details