C# is an object-oriented programming language developed and maintained by Microsoft that is being used to create desktop, web, and mobile apps. Struct, class, and record are three user-defined data types that differ from one another with inherent use cases.
In this article, I will compare and contrast these user-defined data types in C# with some examples to make it easier for you to understand.
Class in C# is a reference type. For reference types, the default equals implementation provides reference equality. Reference equality states that the compared object references relate to the same object. They refer to the exact heap location.
Let’s declare a class in C#.
class Person { public string Name { get; set; } public int Age { get; set; } }
Use the keyword class to create a class in C#. You can specify an access modifier (private, public, protected, internal), if required, for the class.
Let’s create an object for the defined class and assign values for the class properties. Use the dot operator to access the properties of the instance of the class.
Refer to the following code example.
Person person = new Person(); person.Age = 30; person.Name = "Neymar Jr"; Console.WriteLine(person.Name+" "+person.Age); // Neymar Jr 30
Let’s now create another object for the same class person using the first object created.
var person1 = person; person1.Age = 29; Console.WriteLine(person.Name+" "+person.Age); // Neymar Jr 29 Console.WriteLine(person1.Name+" "+person1.Age); // Neymar Jr 29
Here, though we update the age property of the object person1, the same property of the person object is updated. This is due to the fact that class in C# is a reference type. That is, both objects refer to the same place in the memory where actual data is stored.
In other words, object references will be saved in a stack memory, and values will be saved in a heap memory. In this example, two object references represent one value in the heap memory. So, we get the same value for the properties of the two objects.
The struct data structure is similar to the class but is a value data type. Its instances and objects are stored on a stack. It is used to encapsulate a small group of related variables.
Let’s declare a struct in C#.
struct Person { public string Name { get; set; } public int Age { get; set; } }
Use the struct keyword to declare the struct (structure) in C#. You can use access modifiers, if required.
Let’s create an object of the struct Person.
Person person = new Person();
Now, access the struct’s properties and assign them values using the dot operator, as in the code example.
Person person = new Person(); person.Age = 30; person.Name = "Neymar Jr"; Console.WriteLine(person.Name+" "+person.Age); // Neymar Jr 30
We have created a struct object for the struct person. Now, create another struct object using the object created in the previous code.
var person1 = person; person1.Age = 29; Console.WriteLine(person.Name+" "+person.Age); // Neymar Jr 30 Console.WriteLine(person1.Name+" "+person1.Age); // Neymar Jr 29
The struct object person1 was created from the object person. The change in the value of the property of person1 did not affect the value of the object person. This is because struct is a value type of data structure. They represent different locations in the memory for each object.
The record type was introduced in version 9.0 of C#. It provides an immutability feature with which to work. So, record types are immutable by default. The record is also a reference type that behaves identically to the value type when it relates to value equality.
Let’s declare a record in C#.
public record Person( string Name, int Age );
Now, create a record object. When creating the object for the record, you must pass values as parameters to the constructor.
Person person = new Person("Neymar Jr", 30); Console.WriteLine(person.Name+" "+person.Age); // Neymar Jr 30
You can also access properties of the record using the dot operator, as in the code example.
You can print the names and values of properties just by using the object, like in the following code, for the record type.
Console.WriteLine(person); // Person { Name = Neymar Jr, Age = 30 }
We can make properties of a class immutable using the init property, so we cannot change the values of the properties.
class Person { public string Name { get; init; } public int Age { get; init; } }
We have to create a new object whenever we need to assign values to the properties declared with init. These properties that can be set on a class only once during object initialization are known as init-only properties. Moreover, if you attempt to set them again later, a C# compiler error will occur.
When we need to create hundreds of properties using this concept, it won’t be easy to manage. But we can prevent this issue by using the record type with the With expression.
Refer to the following code.
var person1 = person with {Age = 29}; Console.WriteLine(person); // Person { Name = Neymar Jr, Age = 30 } Console.WriteLine(person1); // Person { Name = Neymar Jr, Age = 29 }
In this article, we discussed three user-defined data types, class, struct, and record, and when to use each of them in your code. I hope you found this article helpful. Please share your feedback in the comments below.
Thank you for reading!
Syncfusion provides more than 1,800 components and frameworks to ease app development on various platforms. We encourage you to evaluate the components and use them as needed for application development:
Blazor | Flutter |
ASP.NET Core | ASP.NET MVC |
ASP.NET Web Forms | JavaScript |
Angular | React |
Vue | jQuery |
.NET MAUI | Xamarin |
UWP | WinForms |
WPF | WinUI |
Excel Library | PDF Library |
Word Library | PowerPoint Library |
If you have any questions or comments, you can contact us through our support forums, support portal, or feedback portal. As always, we are happy to assist you!