In the previous articles, we have learned about classes, how to use them and how to create an object as an instance of a class. In this article, we are going to talk about structures which are similar to classes but have some differences as well.

If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.

To download the source code, you can visit Structures in C# Source Code. 

We are going to split this article into the following sections:

Working with Structures

A structure is a value type, in the opposite of a class which is a reference type, and it has its own fields, methods, and constructors like a class.

Maybe you didn’t realize, but we have worked with structures in our previous articles, especially in module 1 C# basics. Int, double, decimal, bool type etc. are all aliases for the structures System.Int32, System.Int64 etc. In a table below, we can see the primitive types and what are they built from (class or structure):

Table - Structures in C#

Structure Declaration

To declare our own structure, we need to use the struct keyword followed by the name of the type and then the body of the structure between two curly braces:

We can create our own constructor to initialize our private fields:

To access our structure we can use this syntax:

Differences Between Classes and Structures

  • The structure is a value type, while the class is a reference type
  • We can’t declare our own default constructor in a structure. That’s because a structure is always generating a default constructor for us. In a class, we can create a default constructor because a class won’t generate then one for us
  • We can initialize fields in our structure by creating a non-default constructor, but we must initialize all of the fields inside that constructor. It is not allowed to left a single field without a value:

Error - Structures in C#

With a class, this is not a case

  • In a class, we can initialize instance fields at their point of declaration. In a structure, we cannot do that:

Instance error - Structures in C#

  • An instance of a class lives on a heap memory while the instance of a structure lives on a stack
  • In a structure, we can create a non-default constructor, but nevertheless, the compiler will always generate the default one. This is not the case with a class.

When to Use Structure Instead of a Class

The general rule that we can follow is that our structures need to be small and simple types and above all immutable. For anything else, we should use a class.

Why is immutability so important?

Well let’s take a look at this example:

If we inspect the result, we will see printed out 10 and 45. And that is the correct result. But if we change our Test class to be a structure and then inspect the result, we will see 10 and 10.

This can lead to the confusion and problems as well, because the consumer may expect that the ChangeNumber method would modify the Number property because we allowed it in the code. But if we create properties or fields immutably (as read-only in a structure) then we can avoid this kind of confusion. The consumer can assign values to the properties by calling the constructor method but after that those properties need to stay immutable.

Conclusion

In this article, we have learned:

  • What is structure and how to create one
  • What are the limitations while using structures
  • When to use structures in your code

In the next article, we are going to talk about Enumerations in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.