Struct vs. Record vs. Class in C#
Tags: csharp, development, web
Published: 2023-05-18 11:12:50

---
title: Struct vs. Record vs. Class in C#
published: true
date: 2023-05-17 11:50:24 UTC
tags: csharp, development, web
canonical_url: https://www.syncfusion.com/blogs/post/struct-record-class-in-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xv8t4liq91tqg1zi4bi.png
---
[C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language) "Wikipedia Link: C Sharp (programming language)") 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

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#.

```csharp
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.

```csharp
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.

```csharp
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.

### When to use class

- When you need to create complex objects that have state, behavior, and need to be extended by inheritance.
- When implementing interfaces, use abstract classes.
- When you need to take advantage of polymorphism.

## Struct

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#.

```csharp
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**.

```csharp
Person person = new Person();
```

Now, access the struct’s properties and assign them values using the dot operator, as in the code example.

```csharp
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.

```csharp
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.

### When to use struct

- When the scope of the object is small.
- When the data group is small and simple.
- When needing better performance for the system.

## Record

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#.

```csharp
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.

```csharp
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.

```csharp
Console.WriteLine(person); // Person { Name = Neymar Jr, Age = 30 }
```

### Record type using with expression

We can make properties of a class immutable using the **init** property, so we cannot change the values of the properties.

```csharp
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.

```csharp
var person1 = person with {Age = 29};
Console.WriteLine(person); // Person { Name = Neymar Jr, Age = 30 }
Console.WriteLine(person1); // Person { Name = Neymar Jr, Age = 29 }
```

### When to use the record type

- Record types are suitable when your data doesn’t need to change (is immutable).
- The record type will benefit data transfer objects if they have one-way flow.

## Conclusion

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:
<table style="table-layout: fixed; width: 100%;">
<tbody>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="Blazor components library" href="https://www.syncfusion.com/blazor-components" target="_blank" rel="noopener">Blazor</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="Flutter widgets" href="https://www.syncfusion.com/flutter-widgets" target="_blank" rel="noopener">Flutter</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="ASP.NET Core UI Controls Library" href="https://www.syncfusion.com/aspnet-core-ui-controls" target="_blank" rel="noopener">ASP.NET Core</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="ASP.NET MVC UI Controls Library" href="https://www.syncfusion.com/aspnet-mvc-ui-controls" target="_blank" rel="noopener">ASP.NET MVC</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="ASP.NET Web Forms Package" href="https://www.syncfusion.com/jquery/aspnet-web-forms-ui-controls" target="_blank" rel="noopener">ASP.NET Web Forms</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="JavaScript UI Controls Library" href="https://www.syncfusion.com/javascript-ui-controls" target="_blank" rel="noopener">JavaScript</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="Angular Components Library" href="https://www.syncfusion.com/angular-ui-components" target="_blank" rel="noopener">Angular</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="React UI Components Library" href="https://www.syncfusion.com/react-ui-components" target="_blank" rel="noopener">React</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="Vue UI Components Library" href="https://www.syncfusion.com/vue-ui-components" target="_blank" rel="noopener">Vue</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="jQuery Widgets/Components package" href="https://www.syncfusion.com/jquery-ui-widgets" target="_blank" rel="noopener">jQuery</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title=".NET MAUI controls" href="https://www.syncfusion.com/maui-controls" target="_blank" rel="noopener">.NET MAUI</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="Xamarin UI controls" href="https://www.syncfusion.com/xamarin-ui-controls" target="_blank" rel="noopener">Xamarin</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="UWP UI Controls" href="https://www.syncfusion.com/uwp-ui-controls" target="_blank" rel="noopener">UWP</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="WinForms UI Controls" href="https://www.syncfusion.com/winforms-ui-controls" target="_blank" rel="noopener">WinForms</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title="WPF controls and File Format libraries" href="https://www.syncfusion.com/wpf-controls" target="_blank" rel="noopener">WPF</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title="WinUI toolkit" href="https://www.syncfusion.com/winui-controls" target="_blank" rel="noopener">WinUI</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title=".NET Excel Library" href="https://www.syncfusion.com/excel-framework/net" target="_blank" rel="noopener">Excel Library</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title=".NET PDF Library" href="https://www.syncfusion.com/pdf-framework/net" target="_blank" rel="noopener">PDF Library</a></td>
</tr>
<tr style="background: none;">
<td style="text-align: center; border: none !important;" width="312"><a title=".NET Word Library" href="https://www.syncfusion.com/word-framework/net" target="_blank" rel="noopener">Word Library</a></td>
<td style="text-align: center; border: none !important;" width="312"><a title=".NET Word PowerPoint Library" href="https://www.syncfusion.com/powerpoint-framework/net" target="_blank" rel="noopener">PowerPoint Library</a></td>
</tr>
</tbody>
</table>
If you have any questions or comments, you can contact us through our [support forums](https://www.syncfusion.com/forums "Syncfusion Support Forums"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback "Syncfusion Feedback Portal"). As always, we are happy to assist you!

## Related blogs

- [5 Features in C# 10 Every Developer Should Know](https://www.syncfusion.com/blogs/post/5-features-in-c-sharp-10-every-developer-should-know.aspx "Blog: 5 Features in C# 10 Every Developer Should Know")
- [Pattern Matching in C# for Beginners](https://www.syncfusion.com/blogs/post/pattern-matching-in-c-for-beginners.aspx "Blog: Pattern Matching in C# for Beginners")
- [Understanding C# Anonymous Types](https://www.syncfusion.com/blogs/post/understanding-csharp-anonymous-types.aspx "Blog: Understanding C# Anonymous Types")
- [Get Started with Tuples in C#: A Beginner’s Handbook](https://www.syncfusion.com/blogs/post/working-with-tuple-in-csharp.aspx "Blog: Get Started with Tuples in C#: A Beginner’s Handbook")