Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an init-only property named Name.
C Sharp (C#)
public string Name { get; [1]; } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' allows changing the property anytime, not just during initialization.
Using 'readonly' is not valid for properties.
✗ Incorrect
The init accessor allows setting the property only during object initialization.
2fill in blank
mediumComplete the code to create a record with an init-only property Age.
C Sharp (C#)
public record Person { public int Age { get; [1]; } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' allows changing the property after creation.
Using 'private set' restricts setting even during initialization.
✗ Incorrect
Records often use init to make properties immutable after creation.
3fill in blank
hardFix the error in the property declaration to make it init-only.
C Sharp (C#)
public string Title { get; [1]; } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving 'set' allows modification anytime.
Using 'readonly' is invalid for properties.
✗ Incorrect
Changing set to init makes the property settable only during initialization.
4fill in blank
hardFill both blanks to define an init-only property with a private getter.
C Sharp (C#)
public string Description { [1] get; [2] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'init' allows modification after initialization.
Making getter public when it should be private.
✗ Incorrect
The property has a private getter and an init-only setter.
5fill in blank
hardFill all three blanks to create a record with an init-only property and a constructor.
C Sharp (C#)
public record Book [1] { public string Author { get; [2]; } = [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'init' allows property changes after creation.
Omitting the constructor parentheses.
Not providing a default value.
✗ Incorrect
The record has a parameterless constructor, an init-only property, and a default value.