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

Why records were introduced in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Record Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Records in C#
Why were records introduced in C#?
ATo replace classes for all object-oriented programming needs.
BTo improve performance of loops and arrays.
CTo provide a concise way to create immutable data objects with value-based equality.
DTo allow multiple inheritance in C#.
Attempts:
2 left
💡 Hint
Think about how data objects are compared and how easy it is to create them.
Predict Output
intermediate
2:00remaining
Output of Record Equality Check
What is the output of this C# code?
C Sharp (C#)
public record Person(string Name, int Age);

var p1 = new Person("Alice", 30);
var p2 = new Person("Alice", 30);
Console.WriteLine(p1 == p2);
ATrue
BFalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Records compare data, not references.
Predict Output
advanced
2:00remaining
Immutability Behavior of Records
What will be the output of this C# code?
C Sharp (C#)
public record Point(int X, int Y);

var p1 = new Point(1, 2);
var p2 = p1 with { X = 3 };
Console.WriteLine($"p1: {p1.X}, {p1.Y}");
Console.WriteLine($"p2: {p2.X}, {p2.Y}");
A
p1: 1, 2
p2: 1, 2
B
p1: 3, 2
p2: 3, 2
CCompilation error
D
p1: 1, 2
p2: 3, 2
Attempts:
2 left
💡 Hint
Records support non-destructive mutation with 'with' expressions.
🔧 Debug
advanced
2:00remaining
Identify the Error in Record Declaration
What error does this C# code produce?
C Sharp (C#)
public record Person(string Name, int Age)
{
    public void Deconstruct(out string name, out int age)
    {
        name = Name;
        age = Age;
    }

    public Person(string name) : this(name, 0)
    {
    }
}
ANo error, code compiles fine.
BCS1002: ; expected.
CCS0120: An object reference is required.
DCS8852: Primary constructor and explicit constructor cannot coexist.
Attempts:
2 left
💡 Hint
Records with primary constructors cannot have explicit constructors without chaining.
🧠 Conceptual
expert
3:00remaining
Why Prefer Records Over Classes for Data Models?
Which reason best explains why records are preferred over classes for data models in C#?
ARecords allow multiple inheritance unlike classes.
BRecords provide built-in value equality and immutability, reducing bugs in data comparisons.
CRecords execute faster than classes in all scenarios.
DRecords automatically generate user interfaces.
Attempts:
2 left
💡 Hint
Think about how data objects are compared and changed.