---
title: "Understanding struct in C#"  
description: "In C#, struct (short for structure) is a value type used to store related data under a single unit. It's similar to a class, but with some key differe"  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/articles/339100/understanding-struct-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Understanding struct in C#

In C#, `struct` (short for **structure**) is a **value type** used to store [related data](https://www.mindstick.com/interview/22990/what-is-lazy-eager-and-explicit-loading-of-related-data-in-entity-frameworks) under a single unit. It's similar to a class, but with some key [differences](https://yourviews.mindstick.com/view/85280/comparative-differences-dell-vs-hp-which-one-is-better) in behavior and use cases.

## What is a Structure?

A **structure** is a user-defined type that groups **different variables** under one name. Structs are best used for small data [containers](https://www.mindstick.com/interview/2194/which-containers-use-a-flowlayout-as-their-default-layout-in-java) that don’t require [inheritance](https://www.mindstick.com/articles/1810/objective-c-inheritance) or complex behaviors.

```cs
struct StructName
{
    // Fields
    public int field1;
    public string field2;

    // Constructor
    public StructName(int f1, string f2)
    {
        field1 = f1;
        field2 = f2;
    }

    // Methods
    public void Display()
    {
        Console.WriteLine($"Field1: {field1}, Field2: {field2}");
    }
}
```

## Example: Defining and Using a Struct

```cs
using System;

struct Employee
{
    public int ID;
    public string EmployeeName;

    public Employee(int id, string name)
    {
        ID = id;
        EmployeeName = name;
    }

    public void Display()
    {
        Console.WriteLine($"ID: {ID}, Name: {EmployeeName}");
    }
}

class Program
{
    static void Main()
    {
        Employee emp1 = new Employee(101, "Alice");
        emp1.Display();
    }
}
```

## Output:

```plaintext
ID: 101, Name: Alice
```

## Struct vs Class

| Feature | `struct` | `class` |
| --- | --- | --- |
| Type | Value type | [Reference type](https://www.mindstick.com/interview/235/what-is-difference-between-value-types-and-reference-types-in-vb-dot-net-or-c-sharp-value-types-vs-reference-types) |
| [Memory allocation](https://www.mindstick.com/forum/159479/what-are-the-differences-between-new-and-malloc-for-memory-allocation-in-c-plus-plus) | Stack | Heap |
| Inheritance | Not supported | Supported |
| Performance | Faster for small data | Suitable for complex models |
| Null [assignment](https://www.mindstick.com/interview/608/what-is-the-difference-between-assignment-and-initialization) | Not allowed (unless nullable) | Allowed |
| [Default constructor](https://www.mindstick.com/forum/34531/default-constructor) | Not allowed (you define it) | Provided automatically |

## When to Use Struct?

Use a struct when:

1. You’re representing **simple objects** (like a point, color, coordinate).
2. You want **value semantics** (copying data instead of references).
3. You don’t need inheritance or polymorphism.

## Additional Notes

1. Structs can **implement interfaces**, but **cannot inherit** from another struct or class.
2. You can use `Nullable<T>` or `T?` with structs if you want to allow `null` values.
3. Structs are **copied by value**, not by reference. So changes made to one copy don't affect the original.

## Mini Quiz

```cs
struct Point {
    public int X, Y;
}

Point p1 = new Point();
p1.X = 5;
Point p2 = p1; // Now it assign
p2.X = 10;
Console.WriteLine(p1.X);
```

#### Summery

| Concept | [Explanation](https://answers.mindstick.com/blog/94/neural-network-simple-explanation-with-real-life-examples) |
| --- | --- |
| `struct` keyword | Defines a structure |
| Value type | Stored on the stack |
| No inheritance | Cannot inherit from classes or other structs |
| Lightweight | Best for small, immutable data |
| Copy behavior | Passed and assigned **by value** |

## Read More

[Structure types - C# reference](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct)

---

Original Source: https://www.mindstick.com/articles/339100/understanding-struct-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
