C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 30

Explain what records are in C#.

Answer:

Records are a special type of class introduced in C# 9.0, designed to simplify the creation of immutable data objects. They provide a concise syntax for defining classes whose primary purpose is to store data. Records are particularly useful for scenarios where you need to work with immutable data and value equality.

Key Features of Records

  1. Immutable by Default:

    • Records are immutable by default, meaning that once an instance is created, its data cannot be changed. You can achieve immutability by using the init accessor for properties.
  2. Value Equality:

    • Records provide value-based equality by default. Two record instances are considered equal if all their properties have the same values, unlike classes where reference equality is used by default.
  3. Concise Syntax:

    • Records provide a concise syntax for defining data objects, reducing boilerplate code for constructors, property definitions, and equality members.
  4. With-Expressions:

    • Records support with-expressions, allowing you to create a copy of an existing record with some properties modified.

Basic Syntax

Defining a Record

Example:

public record Person(string FirstName, string LastName);

This declaration is equivalent to defining a class with a constructor, read-only properties, and appropriate implementations of Equals, GetHashCode, and ToString.

Using a Record

Example:

public class Program
{
    public static void Main()
    {
        var person1 = new Person("John", "Doe");
        var person2 = new Person("John", "Doe");

        Console.WriteLine(person1 == person2); // True, value equality
        Console.WriteLine(person1); // Output: Person { FirstName = John, LastName = Doe }

        var person3 = person1 with { LastName = "Smith" };
        Console.WriteLine(person3); // Output: Person { FirstName = John, LastName = Smith }
    }
}

Detailed Features

Immutability

By default, properties in records are immutable, meaning their values can only be set during initialization. This is achieved using the init accessor.

Example:

public record Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

Positional Records

Positional records provide a more concise way to define records with a primary constructor.

Example:

public record Person(string FirstName, string LastName);

With-Expressions

With-expressions allow you to create a new record instance based on an existing one with some properties changed.

Example:

var person1 = new Person("John", "Doe");
var person2 = person1 with { LastName = "Smith" };

Value Equality

Records implement value equality, meaning two record instances are considered equal if their properties have the same values.

Example:

var person1 = new Person("John", "Doe");
var person2 = new Person("John", "Doe");
Console.WriteLine(person1 == person2); // True

Advanced Features

Records with Methods

Records can have methods, just like classes.

Example:

public record Person(string FirstName, string LastName)
{
    public void Deconstruct(out string firstName, out string lastName)
    {
        firstName = FirstName;
        lastName = LastName;
    }
}

Inheritance

Records support inheritance, allowing you to create derived records.

Example:

public record Person(string FirstName, string LastName);

public record Employee(string FirstName, string LastName, string Position) : Person(FirstName, LastName);

Non-Positional Records

You can define records without positional parameters, using a more traditional class-like syntax.

Example:

public record Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

Benefits of Records

  1. Immutability:

    • Promotes immutability, which can lead to safer and more predictable code.
  2. Value Equality:

    • Simplifies equality comparisons, making it easier to work with value-based logic.
  3. Concise Syntax:

    • Reduces boilerplate code, making the codebase cleaner and easier to maintain.
  4. Readability:

    • Enhances code readability by clearly indicating that the primary purpose of the class is to store data.

Summary

Records in C# provide a concise and powerful way to define immutable data objects with value-based equality. They simplify the creation of data structures, reduce boilerplate code, and promote best practices in immutability and equality. With features like positional parameters, with-expressions, and support for inheritance, records are a versatile tool for managing data in C#.

Recent job openings