Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Init-only setters in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Init-only Setter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of init-only property assignment
What will be the output of this C# code snippet using init-only setters?
C Sharp (C#)
public class Person {
    public string Name { get; init; }
}

var p = new Person { Name = "Alice" };
Console.WriteLine(p.Name);
AAlice
Bnull
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Init-only properties can be set during object initialization.
Predict Output
intermediate
2:00remaining
Error when setting init-only property after initialization
What happens if you try to set an init-only property after the object is created?
C Sharp (C#)
public class Car {
    public string Model { get; init; }
}

var car = new Car { Model = "Sedan" };
car.Model = "Coupe";
Console.WriteLine(car.Model);
AOutputs "Sedan"
BCompilation error
COutputs "Coupe"
DRuntime exception
Attempts:
2 left
💡 Hint
Init-only properties cannot be assigned outside initialization.
🔧 Debug
advanced
2:00remaining
Why does this init-only setter code fail to compile?
Identify the reason for the compilation error in this code using init-only setters.
C Sharp (C#)
public class Book {
    public string Title { get; init; }
}

var b = new Book();
b.Title = "C# Guide";
Console.WriteLine(b.Title);
ATitle property must be readonly to use init-only
BProperty Title is missing a setter
CInit-only property Title cannot be assigned outside object initialization
DMissing semicolon after object creation
Attempts:
2 left
💡 Hint
Check when init-only properties can be assigned.
🧠 Conceptual
advanced
2:00remaining
Understanding init-only setters with inheritance
Given these classes, what is the output of the program?
C Sharp (C#)
public class Animal {
    public string Species { get; init; }
}

public class Dog : Animal {
    public string Breed { get; init; }
}

var dog = new Dog { Species = "Canine", Breed = "Beagle" };
Console.WriteLine($"{dog.Species} - {dog.Breed}");
ACanine - null
BCompilation error due to init-only in base class
CRuntime exception
DCanine - Beagle
Attempts:
2 left
💡 Hint
Init-only setters work with inheritance and can be set during initialization.
📝 Syntax
expert
2:00remaining
Which option causes a compilation error with init-only setters?
Which of the following code snippets will NOT compile due to incorrect use of init-only setters?
Avar p = new Person { Name = "John" }; p.Name = "Jane";
Bvar p = new Person { Name = "John" }; Console.WriteLine(p.Name);
CPerson p = new() { Name = "John" };
Dvar p = new Person(); var q = p with { Name = "Jane" };
Attempts:
2 left
💡 Hint
Remember when init-only properties can be assigned.

Practice

(1/5)
1. What is the main purpose of init-only setters in C#?
easy
A. To prevent object creation
B. To allow setting properties only during object creation
C. To make methods run faster
D. To allow properties to be changed anytime

Solution

  1. Step 1: Understand init-only setters

    Init-only setters allow properties to be set only when the object is created, not after.
  2. Step 2: Compare options

    Only To allow setting properties only during object creation correctly describes this behavior; others are unrelated or incorrect.
  3. Final Answer:

    To allow setting properties only during object creation -> Option B
  4. Quick Check:

    Init-only setters = set only at creation [OK]
Hint: Init-only means set once during creation only [OK]
Common Mistakes:
  • Thinking init-only allows changes anytime
  • Confusing init-only with readonly fields
  • Assuming init-only affects methods
2. Which of the following is the correct syntax to declare an init-only property in C#?
easy
A. public string Name { get; init; }
B. public string Name { get; set; }
C. public string Name { get; readonly; }
D. public string Name { get; private set; }

Solution

  1. Step 1: Recall init-only syntax

    Init-only properties use init instead of set to allow setting only during initialization.
  2. Step 2: Check options

    public string Name { get; init; } uses { get; init; }, which is correct syntax. Others use set, readonly (invalid here), or private set.
  3. Final Answer:

    public string Name { get; init; } -> Option A
  4. Quick Check:

    Init-only syntax = get + init [OK]
Hint: Init-only uses 'init' keyword, not 'set' [OK]
Common Mistakes:
  • Using 'set' instead of 'init'
  • Trying to use 'readonly' in property accessor
  • Confusing 'private set' with init-only
3. What will happen when you run this code?
public class Person {
    public string Name { get; init; }
}

var p = new Person { Name = "Alice" };
p.Name = "Bob";
Console.WriteLine(p.Name);
medium
A. Prints 'Alice'
B. Runtime exception
C. Prints 'Bob'
D. Compile-time error

Solution

  1. Step 1: Understand init-only property behavior

    The property Name can only be set during object initialization (inside the braces). Setting it later causes a compile error.
  2. Step 2: Analyze the code

    After creating p with Name = "Alice", the code tries to assign p.Name = "Bob" outside initialization, which is invalid.
  3. Final Answer:

    Compile-time error -> Option D
  4. Quick Check:

    Changing init-only after creation = compile error [OK]
Hint: Init-only properties can't be changed after creation [OK]
Common Mistakes:
  • Assuming property can be changed anytime
  • Expecting runtime error instead of compile error
  • Confusing init-only with readonly fields
4. Identify the error in this code snippet:
public class Car {
    public string Model { get; init; }
}

var car = new Car();
car.Model = "Sedan";
medium
A. Missing constructor call
B. Property Model must have a setter, not init
C. Cannot assign init-only property outside object initializer
D. No error, code is correct

Solution

  1. Step 1: Check how init-only properties are set

    Init-only properties must be set during object creation using an initializer, not after.
  2. Step 2: Analyze the code

    The code creates car with default constructor, then tries to set Model property outside initialization, which is invalid.
  3. Final Answer:

    Cannot assign init-only property outside object initializer -> Option C
  4. Quick Check:

    Init-only set only in initializer, not later [OK]
Hint: Set init-only properties inside braces only [OK]
Common Mistakes:
  • Trying to set init-only property after creation
  • Thinking init-only means readonly
  • Ignoring object initializer syntax
5. Given this record declaration:
public record Book {
    public string Title { get; init; }
    public string Author { get; init; }
}

var book1 = new Book { Title = "C# Guide", Author = "Jane" };
var book2 = book1 with { Author = "John" };

Console.WriteLine(book2.Title + ", " + book2.Author);

What is the output?
hard
A. C# Guide, John
B. C# Guide, Jane
C. Compile-time error due to init-only setter
D. Runtime exception

Solution

  1. Step 1: Understand 'with' expression on records

    The 'with' expression creates a copy of book1 but allows changing init-only properties during creation of the new object.
  2. Step 2: Analyze the output

    book2 copies Title as "C# Guide" and sets Author to "John". Printing shows "C# Guide, John".
  3. Final Answer:

    C# Guide, John -> Option A
  4. Quick Check:

    'with' allows changing init-only during copy [OK]
Hint: 'with' can set init-only properties on new record copies [OK]
Common Mistakes:
  • Thinking 'with' cannot change init-only properties
  • Expecting compile error on 'with' usage
  • Confusing init-only with immutable fields