Skip to content
Justice Tech School

Properties

Properties wrap fields with get and set. Use init for set once and required for mandatory values.

Example
var u = new User { Name = "Ada", Age = -5 };
Console.WriteLine($"{u.Name} {u.Age}");

class User
{
    public required string Name { get; init; }
    private int _age;
    public int Age { get => _age; set => _age = value < 0 ? 0 : value; }
}

Exercise

Set once.

{ get; ; }

Quiz Test

What does init allow?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.