In this post, learn when to use a class vs a record vs a struct in C#.
In C#, a class is a reference type that can be used to define an object. It can contain fields, properties, methods, and events. Classes are typically used to define complex types that have both data and behavior. In general, if you need to define a simple data structure, use a record. If you need to define a complex type with behavior, use a class. If you need a lightweight object that holds a few data fields, use a struct.
Here is an example of a simple class in C#:
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. The Method "Bark" writes "Woof woof!" to the console when called.
Dog myDog = new Dog();
myDog.Name = "Fido";
myDog.Age = 3;
myDog.Bark();
This creates an instance of the Dog class, sets its properties and calls the Bark Method.
C# language suports different types of classes. Check out Different Types of Classes in C#.
A record is a new feature introduced in C# 9.0, which is a reference type that is used to define simple data structures. Records are similar to classes, but they have some additional features such as automatic properties, equality, and immutability.
Here is an example of a simple record in C#:
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 are defined with a get and init accessor, which means they can be read and written to only during initialization. The record also have a constructor that accepts x and y as input and assigns it 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).
Note that records are immutable by default, which means once a record instance is created, the properties of the record cannot be changed. If you want to change properties you can use the with keyword to create a new record with updated properties.
Learn more here: Introduction to Record Types in C#
A struct is a value type that can be used to define a lightweight object. Like a class, it can contain fields, properties, methods, and events, but structs are generally used to define small, simple types that hold a few data fields. Structs are stored on the stack, whereas classes are stored on the heap.
Here is an example of a simple struct in C#:
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 struct called "Vector" with two fields, "X" and "Y", and a method "Length". The struct also has a constructor which takes two double inputs and assigns them to the fields. The method "Length" returns the length of the vector using the distance formula.
Vector v1 = new Vector(1, 2);
Console.WriteLine(v1.Length());
This creates an instance of the Vector struct, assigns the values 1 and 2 to X and Y respectively and calls the Length method which returns the length of the vector which is 2.236.
Note that structs are value type and are stored on the stack, which means that when a struct variable is passed to a method, a copy of the struct is made and passed, so the method does not operate on the original variables.
Here is more on Struct in C#.
To learn more about C#, here is a free C# programming book.