When To Use Class VS Record VS Struct in C#?

Class

A class is a reference type in C# that can be used to define an object. The class contains  Fields, properties, methods, and events. Classes are commonly used to define complex types that have data as well as behavior. When defining a complex type with behavior, use a class.  

Example of simple c# class

class Dog
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void Bark()
    {
        Console.WriteLine("Woof woof!");
    }
}

This example defines a class called “Dog” with two properties, “Name” and “Age”, and a method “Bark”. The properties “Name” and “Age” are defined with a get and set accessor, which means they can be read and written to like public variables. When invoked, the Method “Bark” prints “Woof woof!” to the console.

Dog myDog = new Dog();
myDog.Name = "Fido";
myDog.Age = 3;
myDog.Bark();

This creates an instance of the Dog class, sets its properties, and invokes the Bark Method.

Record

A record, which is a reference type used to define simple data structures, is a new feature introduced in C# 9.0. Records are similar to classes, but they have extra features like automatic properties, equality, and immutability.

Example of a simple c# record

record Point
{
    public int X { get; init; }
    public int Y { get; init; }

    public Point(int x, int y) => (X, Y) = (x, y);
}

This example defines a record called “Point” with two properties, “X” and “Y,” both of which are defined with a get and init accessor, implying that they can only be read and written to during initialization. The record also has a constructor that takes x and y as parameters and assigns them to the properties.

Point point = new Point(1, 2);
Console.WriteLine($"({point.X}, {point.Y})");

This creates an instance of the Point record, and assigns the values 1, 2 to X and Y. The WriteLine statement prints the values of the point which is (1,2).

Records are immutable by default, which means that once a record instance is created, its properties cannot be changed. If you want to change the properties, you can create a new record with the updated properties by using the with keyword.

Struct

A struct is a type of value that can be used to specify a lightweight object. Structs, like classes, can have fields, properties, methods, and events, but they are typically used to define small, simple types with a few data fields. Structures are kept on the stack, whereas classes are kept on the heap.

Example of a simple C# struct

struct Vector
{
    public double X;
    public double Y;

    public Vector(double x, double y)
    {
        X = x;
        Y = y;
    }

    public double Length()
    {
        return Math.Sqrt(X * X + Y * Y);
    }
}

This example defines a “Vector” struct with two fields, “X” and “Y,” and a “Length” method. A constructor is also provided for the struct, which takes two double inputs and assigns them to the fields. The “Length” method returns the vector’s length using the distance formula.

Vector v1 = new Vector(1, 2);
Console.WriteLine(v1.Length());

This creates a Vector struct instance, assigns the values 1 and 2 to X and Y, and calls the Length method, which returns the vector’s length, which is 2.236.

structs are value types and are stacked, when a struct variable is passed to a method, a copy of the struct is created and passed, so the method does not operate on the original variables.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories