0
0
C Sharp (C#)programming~10 mins

Init-only setters in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aprivate set
Binit
Cset
Dreadonly
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' allows changing the property anytime, not just during initialization.
Using 'readonly' is not valid for properties.
2fill in blank
medium

Complete 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'
Ainit
Bset
Cprivate set
Dreadonly
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' allows changing the property after creation.
Using 'private set' restricts setting even during initialization.
3fill in blank
hard

Fix 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'
Aset
Breadonly
Cprivate set
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving 'set' allows modification anytime.
Using 'readonly' is invalid for properties.
4fill in blank
hard

Fill 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'
Aprivate
Binit
Cpublic
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'init' allows modification after initialization.
Making getter public when it should be private.
5fill in blank
hard

Fill 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'
A()
Binit
C"Unknown"
Dset
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.