Skip to content

C# Class vs Struct vs Record

C# Class vs Struct vs Record
Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

This article is a general comparison of C# Class vs Struct vs Record. Like many other object-oriented programming languages, C# offers different ways to define data structures, namely classes, structs, and more recently, records. Each of these types has its own unique features and use cases, and understanding the similarities and differences between them can help developers make informed decisions when designing their applications.

Classes

Classes are the most commonly used type of data structure in C#. They are reference types, which means that when an object is created from a class, a reference to that object is created, and the reference is passed around rather than the object itself. Classes can contain data members, which are variables that store data, and methods, which are functions that operate on that data.

Classes are often used to model complex objects with multiple data members and methods. For example, consider a class that represents a person with properties for their name, age, and address:

 

In this example, the Person class has three data members (Name, Age, and Address) and one method (SayHello). We can create an object from this class and call the SayHello method like this:

Output:

 

Structs

Structs are value types, which means that when an object is created from a struct, a copy of that object is created and passed around rather than a reference to it. Structs can also contain data members and methods, just like classes.

Structs are often used to represent small, lightweight objects that don’t require the overhead of a reference type. For example, consider a struct that represents a 2D point:

 

In this example, the Point struct has two data members (X and Y) and one method (Print). We can create a Point object and call the Print method like this:

Output:

You can find a detailed comparison between C# classes ans Structs in this article.

 

Records

Records were introduced in C# 9 as a new type of data structure that combines the best of both classes and structs. Records are reference types, like classes, but they have a simpler syntax and provide built-in functionality for comparing and hashing objects, like structs.

Records are usually used to represent immutable data structures, such as a point in time, a currency amount, or a configuration object. For example, consider a record that represents a date:

In this example, the Date record has three data members (Year, Month, and Day). We can create a Date object like this:

 

Records can also contain methods, just like classes and structs:

In this example, the Date record has a single method (GetMonthName), which returns the name of the month represented by the Month data member. We can call this method like this:

Output:

 

By the way, did you know that we offer a unique online course that boosts your C# career? Check it out here!

 

Similarities and Differences

All three types of data structures in C# can contain data members and methods, but they differ in how they are stored and passed around. Classes are reference types, structs are value types, and records are reference types with built-in functionality for comparing and hashing objects.

Classes are often used to model complex objects, while structs are often used to represent small, lightweight objects. Records are often used to represent immutable data structures.

Use Cases

Classes, structs, and records each have their own use cases. Classes are often used for modeling complex objects, such as a car, a bank account, or a game character. Structs are often used for representing small, lightweight objects, such as a point, a rectangle, or a color. Records are often used for representing immutable data structures, such as a date, a time interval, or a configuration object.

When deciding which type of data structure to use, consider the size and complexity of the object you are modeling, whether the object needs to be immutable, and how the object will be used in your application.

 

Conclusion

In conclusion, classes, structs, and records are all useful data structures in C# with their own unique features and use cases. Understanding the differences between them can help you make informed decisions when designing your applications.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide