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

Why records were introduced 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 record type named Person.

C Sharp (C#)
public [1] Person(string Name, int Age);
Drag options to blanks, or click blank then click option'
Arecord
Bclass
Cstruct
Dinterface
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'record' will create a normal class without value equality.
Using 'struct' creates a value type but lacks record features.
2fill in blank
medium

Complete the code to create a new Person record with name 'Alice' and age 30.

C Sharp (C#)
var person = new Person([1], 30);
Drag options to blanks, or click blank then click option'
A"Alice"
BAlice
Cperson.Name
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string literals causes errors.
Using variable names instead of string literals.
3fill in blank
hard

Fix the error in comparing two Person records for equality.

C Sharp (C#)
bool areEqual = person1 [1] person2;
Drag options to blanks, or click blank then click option'
Aequals
B!=
C==
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using 'equals' is not a valid operator in C#.
4fill in blank
hard

Fill in the blank to create a copy of a Person record with a changed Age.

C Sharp (C#)
var olderPerson = person with [1];
Drag options to blanks, or click blank then click option'
A[ Age = 31 ]
B( Age = 31 )
C< Age = 31 >
D{ Age = 31 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets instead of braces causes syntax errors.
Omitting braces entirely is invalid syntax.
5fill in blank
hard

Fill all three blanks to define a record with positional parameters and override ToString method.

C Sharp (C#)
public [1] Person(string Name, int Age) {
    public override string ToString() => $"[2] is [3] years old.";
}
Drag options to blanks, or click blank then click option'
Arecord
Bclass
CName
DAge
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'record' loses record benefits.
Using incorrect property names in the string causes errors.