Introduction


The name Struct is short for “Structured Data.” While a variable like an int or a bool holds a single piece of information, a Struct allows us to group a set of related data together into one single “Record.”

The family Tree (where it comes from) Understanding Structs helps you understand how programming languages evolved:

  • The C Language: Created Structs first. They were just basic “containers” for data.

  • The C++ Language: Took the idea of Structs and added “behavior” (methods), which became Classes.

  • The C# Language: Uses both. C# was built based on C++, so it kept Structs for simple tasks and Classes for complex ones.

At first glance, a Struct looks almost identical to a Class. You define fields, you use properties, and you can even have methods. However, they are fundamentally different in how the computer handles them.

A. Classes are “Reference Types” When you create a Class, the computer puts the actual data in a large storage area called the Heap

  • You don’t pass the actual object around.
  • You pass a Reference (like a digital “link” or a “pointer”) to that spot in the Heap.

B. Structs are “Value Types” When you create a Struct, the computer defines a Value Type.

  • There are no references or “links.”
  • The data lives exactly where you created it (usually on the Stack).
Memory Impact “Duplication”

This is the most critical difference for a programmer to remember.

  • Passing a Class: When you pass a Class to a function or assign it to a new variable (b = a), you are just sharing the link. Both variables point to the same data.

  • Passing a Struct: When you pass a Struct, the computer duplicates the memory. It makes a physical copy of every single field inside that struct.

FeatureClassStruct
TypeReference TypeValue Type
StorageAllocated on the HeapAllocated on the Stack
Passing AroundYou pass a Link (Reference)You pass a Copy (Duplication)
AnalogyA shared Google Doc (Link)A printed handout (Physical Copy)

“Structs ‘duplicate’ their memory every time you move them, they are very fast for small things like a Coordinate (X, Y) or a Color (R, G, B). But if your ‘record’ is huge, copying it over and over will slow down the computer. This is why we choose carefully between them!”

C# Programming: Creating a Struct

Code comparison At first glance, a struct and a class look like twins. They can both have Properties and Methods.

The Struct Version:

struct TimeStruct
{
    public int Seconds { get; set; }

    public int CalculateMinutes()
    {
        return Seconds / 60;
    }
}

The Class Version:

class TimeClass
{
    public int Seconds { get; set; }

    public int CalculateMinutes()
    {
        return Seconds / 60;
    }
}

The difference between Classes and Structs is most obvious when you pass them into a method. This is where the “hidden copy” either saves you or confuses you!

The ‘Copy’ vs ‘Reference’ concept

Example-1 To understand memory, we have to see what happens when we set one variable equal to another (b = a).

The Struct (Independent Copies)

When you have two Struct variables, they are completely separate. Giving data from s1 to s2 creates a physical duplicate.

// --- THE TEST ---
TimeStruct s1 = new TimeStruct();
s1.Seconds = 120;

TimeStruct s2 = s1; // The data is COPIED into a new box
s2.Seconds = 300;   // Only s2 changes!

// Check the results:
Console.WriteLine(s1.Seconds); // Result: 120 (Original is safe)
Console.WriteLine(s2.Seconds); // Result: 300
Console.WriteLine(s1.CalculateMinutes()); // Result: 2 mins

The Logic: s1 and s2 are two separate buckets. Pouring water from one into another gives you two buckets of water. If you add dye to the second bucket, the first one stays clear.

// --- THE TEST ---
TimeClass c1 = new TimeClass();
c1.Seconds = 120;

TimeClass c2 = c1; // Both variables now POINT to the same data
c2.Seconds = 300;  // Changing c2 affects the same object c1 sees!

// Check the results:
Console.WriteLine(c1.Seconds); // Result: 300 (It changed!)
Console.WriteLine(c2.Seconds); // Result: 300
Console.WriteLine(c1.CalculateMinutes()); // Result: 5 mins

The Logic: c1 and c2 are two remote controls for the same TV. If you change the channel with remote #2, the person holding remote #1 sees the new channel too.


Example-2 In C#, a Struct and a Class might look the same, but they handle memory differently. This is most important when you send them into a method.

The Struct (Value Type)

A Struct is like a Photocopy. When you pass it to a method, the computer makes a brand new copy. If the method changes the copy, the original stays the same.

struct TimeStruct
{
    public int Seconds { get; set; }

    public int CalculateMinutes()
    {
        return Seconds / 60;
    }
}

class Program
{
    static void Main()
    {
        TimeStruct original = new TimeStruct() { Seconds = 120 };

        ChangeStruct(original); // We pass a COPY

        // Output: 120 (The original did NOT change!)
        Console.WriteLine("Original Struct Seconds: " + original.Seconds);
    }

    static void ChangeStruct(TimeStruct copy)
    {
        copy.Seconds = 300; // Only the copy changes
        Console.WriteLine("Inside Method: " + copy.CalculateMinutes() + " mins");
    }
}

The Class (Reference Type)

A Class is like a Spare Key. When you pass it to a method, you are giving it a link (Reference) to the real object. If the method changes it, everyone sees the change.

class TimeClass
{
    public int Seconds { get; set; }

    public int CalculateMinutes()
    {
        return Seconds / 60;
    }
}

class Program
{
    static void Main()
    {
        TimeClass original = new TimeClass() { Seconds = 120 };

        ChangeClass(original); // We pass a LINK (Reference)

        // Output: 300 (The original WAS changed!)
        Console.WriteLine("Original Class Seconds: " + original.Seconds);
    }

    static void ChangeClass(TimeClass realObject)
    {
        realObject.Seconds = 300; // The real object changes
        Console.WriteLine("Inside Method: " + realObject.CalculateMinutes() + " mins");
    }
}

The “Three S” Rule for Structs

In professional C# development, we use Classes 99% of the time. We only choose a Struct if it passes these three tests:

  • Small: The data is tiny (like a Vector2D with just X and Y). Copying 2 numbers is fast. Copying a Player with 50 variables is slow!

  • Simple: It is just a “Record” or a “Bag of Data.” It doesn’t need to “think” or have complex AI.

  • Safe: You want each variable to be independent. You don’t want a method to accidentally change your original data.

Real-World Examples
Use a Struct for…Use a Class for…
Coordinates: (X, Y, Z) positions.The Player: The character moving around.
Colors: (R, G, B) values.Inventory System: Managing items and gold.
Dates: (Day, Month, Year).Enemy AI: Logic for how a monster attacks.
Dimensions: Width and Height.Game Manager: The system that runs the level.