What is a Record Type in C#?

πŸ’‘ Concept: Record Type

In C#, a record is a reference type introduced in C# 9. It is used to define immutable data models with value-based equality. Records are ideal for scenarios where you want to represent data structures that do not change after creation.

πŸ“˜ Quick Intro

Records are syntactic sugar that make it easy to create classes where equality is determined by content rather than reference. They're perfect for use in data transfer objects, configuration models, and functional-style programming.

🧠 Analogy

Think of a record like a passportβ€”two passports with the same name, date of birth, and ID number are considered equal regardless of where they’re stored. With classes, equality is like comparing two suitcasesβ€”regardless of contents, only their memory address matters.

πŸ”§ Technical Explanation

  • πŸ“Œ Records are reference types but use value-based equality by default.
  • πŸ” Ideal for immutability and functional patterns.
  • 🧱 Supports non-destructive mutation via `with` expressions.
  • πŸ“₯ Automatically provides deconstruction and built-in printing.
  • πŸ†• Introduced in C# 9 and enhanced in C# 10/11 with features like `record struct`.

🎯 Use Cases

  • βœ… DTOs (Data Transfer Objects) in APIs or services.
  • βœ… Immutable configuration/state objects in applications.
  • βœ… Simplifying models in pattern matching and LINQ queries.
  • βœ… Domain modeling in functional programming styles.

πŸ’» Code Example

public record Person(string FirstName, string LastName);

var person1 = new Person(""Alice"", ""Smith"");
var person2 = new Person(""Alice"", ""Smith"");

Console.WriteLine(person1 == person2); // Output: True

var updated = person1 with { LastName = ""Johnson"" };
Console.WriteLine(updated); // Output: Person { FirstName = Alice, LastName = Johnson }

❓ Interview Q&A

Q1: What is a record in C#?
A: A record is a reference type with value-based equality introduced in C# 9.

Q2: Are records value types?
A: No, records are reference types by default.

Q3: What is value-based equality?
A: It means equality is determined by the content, not memory reference.

Q4: Can you mutate a record after creation?
A: Records are immutable by default, but you can use `with` expressions.

Q5: What is a `with` expression in records?
A: It allows you to create a new record with some properties changed.

Q6: What is the benefit of using records for DTOs?
A: Cleaner syntax, immutability, and structural equality.

Q7: Do records support inheritance?
A: Yes, but it's rarely used and discouraged in many cases.

Q8: Can records have methods?
A: Yes, like classes, they can contain methods and other members.

Q9: What is a positional record?
A: A record defined using the primary constructor syntax.

Q10: How do record types help in pattern matching?
A: They provide built-in deconstructors useful for matching values.

πŸ“ MCQs

Q1. What keyword is used to define a record?

  • class
  • struct
  • record
  • readonly

Q2. Which version of C# introduced records?

  • C# 7
  • C# 8
  • C# 9
  • C# 10

Q3. What is the default equality behavior of records?

  • Reference-based
  • Hash-based
  • Value-based
  • None

Q4. What does the 'with' keyword do in records?

  • Assigns values
  • Compares objects
  • Creates a copy with changes
  • Initializes arrays

Q5. Are records mutable by default?

  • Yes
  • No
  • Only in C# 11
  • Depends on constructor

Q6. Can records have methods?

  • Yes
  • No
  • Only static
  • Only private

Q7. What type of equality do classes use by default?

  • Value-based
  • Field-based
  • Reference-based
  • Index-based

Q8. What does 'deconstructing a record' mean?

  • Deleting fields
  • Creating objects
  • Breaking into properties
  • Refactoring

Q9. What is a positional record?

  • A subclass of struct
  • Defined with constructor syntax
  • An obsolete syntax
  • Record with fields only

Q10. Which keyword helps create immutable records?

  • set
  • readonly
  • init
  • static

πŸ’‘ Bonus Insight

Records bring C# closer to modern programming paradigms like immutability and functional design. They're lightweight, expressive, and a great alternative to manually implemented DTOs or POCOs. Consider using records when working with APIs, stateful objects, or even database projections.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: