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

Read-only and write-only properties 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 make the property read-only.

C Sharp (C#)
public class Person {
    private string name;
    public string Name {
        get { return [1]; }
    }
}
Drag options to blanks, or click blank then click option'
Aname
BName
Cthis.name
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the property itself causes infinite recursion.
Returning null does not provide the stored value.
2fill in blank
medium

Complete the code to make the property write-only.

C Sharp (C#)
public class Account {
    private decimal balance;
    public decimal Balance {
        set { [1] = value; }
    }
}
Drag options to blanks, or click blank then click option'
ABalance
Bthis.Balance
Cvalue
Dbalance
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to the property itself causes infinite recursion.
Assigning to 'value' is invalid.
3fill in blank
hard

Fix the error in the read-only property to avoid infinite recursion.

C Sharp (C#)
public class Product {
    private int stock;
    public int Stock {
        get { return [1]; }
    }
}
Drag options to blanks, or click blank then click option'
A0
Bstock
Cthis.Stock
DStock
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the property itself causes infinite recursion.
Returning a constant like 0 does not reflect the actual value.
4fill in blank
hard

Fill both blanks to create a read-only property with a private setter.

C Sharp (C#)
public class Employee {
    public int Id { get; [1] }
    public string Name { get; [2] }
}
Drag options to blanks, or click blank then click option'
Aprivate set
Bset
Cprivate get
Dpublic set
Attempts:
3 left
💡 Hint
Common Mistakes
Using public set makes the property writable from outside.
Using private get is invalid for read-only properties.
5fill in blank
hard

Fill all three blanks to create a write-only property with a private getter.

C Sharp (C#)
public class Secret {
    private string data;
    public string Data {
        [1] { return data; }
        [2] { data = value; }
    }
    private string GetData() {
        return [3];
    }
}
Drag options to blanks, or click blank then click option'
Aprivate get
Bset
Cdata
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using public get makes the property readable from outside.
Returning the property itself causes recursion.