0Pricing
C# Academy · Lesson

The required Modifier

Force callers to set essential properties.

The required Modifier is a free C# Academy lesson on CoddyKit — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

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, and the C# Academy course includes 4 lessons in total. 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.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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