C# Academy · Lesson

The required Modifier

Force callers to set essential properties.

Lesson 2 of 413 steps

The required Modifier is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

The required Modifier

The required modifier forces callers to set a property (or field) when creating an object. It moves "you forgot a value" errors from runtime to compile time.

using System;

class Person {
    public required string Name { get; init; }
}

var p = new Person { Name = "Ada" };
Console.WriteLine(p.Name); // Ada

Omitting a required Member Fails to Compile

If a caller leaves out a required member, the compiler reports an error before the program ever runs.

using System;

class Person {
    public required string Name { get; init; }
}

// var bad = new Person { }; // compile error: Name is required
var good = new Person { Name = "Lin" };
Console.WriteLine(good.Name); // Lin

required with init

required pairs naturally with init: the value must be supplied at construction and cannot change afterward.

using System;

class Config {
    public required string Url { get; init; }
    public int Timeout { get; init; } = 30;
}

var c = new Config { Url = "https://api" };
Console.WriteLine(c.Url + " " + c.Timeout); // https://api 30

required with set

You can mark a settable property required too. It must be initialized but can still be changed later.

using System;

class Box {
    public required int Size { get; set; }
}

var b = new Box { Size = 5 };
b.Size = 10; // still mutable
Console.WriteLine(b.Size); // 10

required Fields

Fields can be required as well, not just properties.

using System;

class Token {
    public required string Value;
}

var t = new Token { Value = "abc123" };
Console.WriteLine(t.Value); // abc123

Constructors and required

If a constructor sets a required member, callers using that constructor no longer must set it. Use [SetsRequiredMembers] to tell the compiler.

using System;
using System.Diagnostics.CodeAnalysis;

class Person {
    public required string Name { get; init; }
    [SetsRequiredMembers]
    public Person(string name) => Name = name;
}

Console.WriteLine(new Person("Sam").Name); // Sam

Mixing required and Optional

Only the members you mark are mandatory; others remain optional with their defaults.

using System;

class Account {
    public required string Owner { get; init; }
    public decimal Balance { get; init; } = 0m;
}

var a = new Account { Owner = "Kai" };
Console.WriteLine(a.Owner + " " + a.Balance); // Kai 0

required in Inheritance

Required members are inherited. A derived class can add its own required members, and all must be satisfied.

using System;

class Base { public required int Id { get; init; } }
class Item : Base { public required string Name { get; init; } }

var i = new Item { Id = 1, Name = "Pen" };
Console.WriteLine(i.Id + " " + i.Name); // 1 Pen

Replacing Heavy Constructors

required + init often replaces big constructors: you get mandatory members with the readable object-initializer syntax.

using System;

class Email {
    public required string To { get; init; }
    public required string Subject { get; init; }
    public string Body { get; init; } = "";
}

var e = new Email { To = "a@b.com", Subject = "Hi" };
Console.WriteLine(e.To + " | " + e.Subject); // a@b.com | Hi

Self-Documenting Intent

Marking members required documents which values are essential, making the type easier to use correctly.

using System;

class Registration {
    public required string Username { get; init; }
    public required string Password { get; init; }
    public bool Newsletter { get; init; }
}

var r = new Registration { Username = "ada", Password = "x" };
Console.WriteLine(r.Username); // ada

Putting It Together

A type that combines required, optional, and validated members.

using System;

class Server {
    public required string Host { get; init; }
    public int Port { get; init; } = 8080;
    public string Address => Host + ":" + Port;
}

var s = new Server { Host = "localhost" };
Console.WriteLine(s.Address); // localhost:8080

Quick Check

Confirm what the required modifier guarantees.

Recap

You learned the required modifier.

  • It forces callers to initialize the member at creation, checked by the compiler.
  • It pairs with init for mandatory immutable members.
  • [SetsRequiredMembers] lets a constructor satisfy the requirement.
  • Required members are inherited.
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “The required Modifier” lesson free?

Yes — the full text of “The required Modifier” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.

What will I learn in “The required Modifier”?

Force callers to set essential properties. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “The required Modifier” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. init-Only Setters
  2. The required Modifier
  3. Immutable Object Patterns
  4. with Expressions on Records
← Back to C# Academy