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

Why encapsulation matters in C Sharp (C#) - Test Your Understanding

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

Complete the code to declare a private field in a class.

C Sharp (C#)
private int [1];
Drag options to blanks, or click blank then click option'
Avoid
Bpublic
Cage
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using a keyword like 'public' or 'class' as a field name.
Leaving the field name blank.
2fill in blank
medium

Complete the code to create a public method that returns the private field value.

C Sharp (C#)
public int GetAge() {
    return [1];
}
Drag options to blanks, or click blank then click option'
AGetAge
Bage
Cthis
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the method name instead of the field.
Using 'void' as a return value.
3fill in blank
hard

Fix the error in the setter method to correctly assign the value to the private field.

C Sharp (C#)
public void SetAge(int value) {
    [1] = value;
}
Drag options to blanks, or click blank then click option'
Athis.value
Bvalue
CSetAge
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the parameter to itself.
Using 'this.value' which is incorrect here.
4fill in blank
hard

Fill both blanks to create a property that encapsulates the private field.

C Sharp (C#)
public int Age {
    get { return [1]; }
    set { [2] = value; }
}
Drag options to blanks, or click blank then click option'
Aage
BAge
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name inside getter or setter instead of the field.
Assigning the parameter to itself.
5fill in blank
hard

Fill all three blanks to complete the class with encapsulation for the age field.

C Sharp (C#)
class Person {
    private int [1];

    public int [2] {
        get { return [3]; }
        set { [3] = value; }
    }
}
Drag options to blanks, or click blank then click option'
Aage
BAge
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name inside its own getter or setter causing infinite recursion.
Not declaring the private field.