Understanding Struct Type in C# - A Comprehensive Guide

Category > CSHARP || Published on : Tuesday, March 7, 2023 || Views: 322 || C# Struct Value Type Pass by Value Mutable Struct Immutable Struct


This article provides a detailed guide on the struct type in C#, including what it is, how to create and use it, pass by value, and mutable vs immutable structs.

In C#, a struct is a value type that allows you to create custom data types that can be used to hold a small set of related data. Unlike classes, which are reference types, structs are allocated on the stack, which means they are lightweight and faster to create and destroy. In this article, we'll explore the struct type in C# and provide examples to demonstrate how to use it.

Creating a Struct in C#

To create a struct in C#, you use the struct keyword, followed by the name of the struct and its body, which is enclosed in curly braces { }. Here's an example:

struct Point
{
    public int X;
    public int Y;
}

In this example, we've created a struct named Point that has two public fields, X and Y, both of type int. The public keyword specifies that these fields can be accessed from outside the struct.

Using a Struct in C#

To use a struct in C#, you create an instance of it using the new keyword, like this:

Point point = new Point();
point.X = 10;
point.Y = 20;

In this example, we've created a new instance of the Point struct and assigned values to its X and Y fields.

You can also use object initializer syntax to create and initialize a struct in a single line, like this:

Point point = new Point { X = 10, Y = 20 };

This is equivalent to the previous example, but it's shorter and more concise.

Pass by Value

When you pass a struct as an argument to a method or assign it to a variable, it is passed by value. This means that a copy of the struct is created and passed to the method or assigned to the variable. Any changes made to the struct inside the method or the variable will not affect the original struct.

Here's an example:

static void Main()
{
    Point point = new Point { X = 10, Y = 20 };
    SetXToZero(point);
    Console.WriteLine(point.X); // Output: 10
}

static void SetXToZero(Point point)
{
    point.X = 0;
}

In this example, we've created a new Point struct and assigned it to the point variable. We then pass this Point struct to the SetXToZero method, which sets its X field to zero. However, when we print the value of point.X after the method call, we see that it still has its original value of 10. This is because the SetXToZero method received a copy of the Point struct, so any changes made to it inside the method did not affect the original Point struct.

Mutable vs Immutable Structs

In C#, you can create either mutable or immutable structs. Mutable structs are structs whose fields can be modified after they are created, while immutable structs are structs whose fields cannot be modified after they are created.

Here's an example of a mutable struct:

struct MutablePoint
{
    public int X;
    public int Y;

    public void Translate(int dx, int dy)
    {
        X += dx;
        Y += dy;
    }
}

In this example, we've created a struct named MutablePoint that has two public fields, X and Y, both of type int. We've also added a Translate method