Skip to Content
All posts

Classes vs. Structs vs. Records In C#

2 min read ·  — #csharp-interview#junior#oop

Classes vs. Structs vs. Records In C#

In the world of object-oriented programming (OOP), it's crucial to understand the building blocks that help you structure and manipulate data in a robust and meaningful way. In C#, a statically-typed, multi-paradigm programming language, three such critical constructs are classes, structs, and records. They form the backbone of your applications, allowing you to model real-world entities, encapsulate data, and create complex data structures. In preparing for a technical interview that involves C#, a thorough understanding of these constructs will go a long way in showcasing your grasp of the language and OOP concepts. In this post, we will delve into each of these three constructs, explaining what they are and how they are used, with examples for each one.

Classes

A class is a blueprint for creating objects (a particular data structure), providing initial values for its properties ( member variables or attributes), and implementations of behavior (member functions or methods). The class is the most fundamental of C#'s types.

Here's a simple example:

public class Employee
{
    // These are the properties
    public string Name { get; set; }
    public int Age { get; set; }

    // This is a method
    public void DisplayInfo()
    {
        Console.WriteLine("Name: " + Name);
        Console.WriteLine("Age: " + Age);
    }
}

// Usage
Employee emp = new Employee();
emp.Name = "John";
emp.Age = 30;
emp.DisplayInfo();

Structs

A struct is a value type, while a class is a reference type. This means that when a struct is assigned to a new variable, a copy of the value is made. However, when a class is assigned to a new variable, the reference is copied, not the actual value itself.

Structs are particularly useful for small data structures that have value semantics. Complex numbers, points in a plane, or key-value pairs in a dictionary are all good examples of structs.

Here's a simple example:

public struct Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

// Usage
Point p1 = new Point();
p1.X = 10;
p1.Y = 20;

Point p2 = p1; // p2 is a separate copy of p1

Records

Records, introduced in C# 9.0, are a way to make value-based classes and structs more concise. Records can be thought of as simple containers for data. The big advantage of records over regular classes and structs is that they have value-based equality semantics, meaning two record objects are equal if their properties are all equal.

Here's a simple example:

public record Student(string Name, int Age);

// Usage
var student1 = new Student("John", 20);
var student2 = new Student("John", 20);

// Outputs: True, because record provides value-based equality
Console.WriteLine(student1 == student2);

The main takeaway here is that classes are reference types and offer more flexibility, structs are value types and are more lightweight, while records offer the immutability and value-based equality semantics, reducing the boilerplate code.

Hope these examples and explanation help. Do note that these are just basic introductions. These types can have methods, implement interfaces, and more. It is important to understand where to use each one and why.