Challenge - 5 Problems
Record Equality Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of comparing two records with same values
What is the output of this C# code snippet?
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);
Attempts:
2 left
💡 Hint
Records in C# have built-in value equality.
✗ Incorrect
Records automatically implement value equality, so two records with the same values are equal using == operator.
❓ Predict Output
intermediate2:00remaining
Output of comparing record and class with same properties
What is the output of this C# code snippet?
C Sharp (C#)
public record PersonRecord(string Name, int Age); public class PersonClass { public string Name { get; init; } public int Age { get; init; } } var r = new PersonRecord("Bob", 25); var c = new PersonClass { Name = "Bob", Age = 25 }; Console.WriteLine(r.Equals(c));
Attempts:
2 left
💡 Hint
Records and classes have different equality implementations.
✗ Incorrect
Record equality checks type and values; comparing to a class instance returns false.
🔧 Debug
advanced2:30remaining
Why does this record equality fail?
Consider this code snippet. Why does the equality check return false?
public record Point(int X, int Y);
var p1 = new Point(1, 2);
var p2 = new Point(1, 3);
Console.WriteLine(p1 == p2);
C Sharp (C#)
public record Point(int X, int Y); var p1 = new Point(1, 2); var p2 = new Point(1, 3); Console.WriteLine(p1 == p2);
Attempts:
2 left
💡 Hint
Check the values of both records' properties.
✗ Incorrect
Records compare all properties for equality; since Y differs, the records are not equal.
📝 Syntax
advanced2:00remaining
Which record declaration is invalid?
Which of the following C# record declarations will cause a compilation error?
Attempts:
2 left
💡 Hint
Check if the record has invalid field declarations.
✗ Incorrect
Records cannot have public fields; they must use properties. Option C declares a public field 'Year', causing a compilation error.
🚀 Application
expert2:30remaining
How many items in the HashSet after adding records?
Given this code, how many items will the HashSet contain after execution?
C Sharp (C#)
public record Employee(string Id, string Name); var set = new HashSet<Employee>(); set.Add(new Employee("E01", "John")); set.Add(new Employee("E01", "John")); set.Add(new Employee("E02", "Jane")); set.Add(new Employee("E01", "John")); Console.WriteLine(set.Count);
Attempts:
2 left
💡 Hint
HashSet uses equality to avoid duplicates.
✗ Incorrect
Records have value equality, so duplicates with same values are not added. The set contains two unique employees.