---
title: "When to use Struct over Class in C#"  
description: "When to use Struct over Class in C#"  
author: "Steilla Mitchel"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/forum/160700/when-to-use-struct-over-class-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# When to use Struct over Class in C#

When to use [Struct](https://www.mindstick.com/blog/83/struct-in-c-sharp-dot-net) over [Class in C#](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example)

## Replies

### Reply by Ravi Vishwakarma

In C#, both **structs** and **classes** are used to define types, but they have distinct differences and are used in different scenarios.

#### Key Differences Between Structs and Classes

## Value Type vs. Reference Type:

- **Structs** are value types and are stored on the stack. When you assign a struct to a new variable, a copy of the value is made.
- **Classes** are reference types and are stored on the heap. When you assign a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) to a new variable, you are copying the reference to the same object.

## Memory Management:

- **Structs** are automatically deallocated when they go out of scope.
- **Classes** are managed by the garbage collector, which means their lifetime is controlled by the garbage collector.

## Inheritance:

- **Structs** do not support inheritance **(they cannot inherit from another struct or class, nor can they be a base for other structs or classes)**. However, they can implement interfaces.
- **Classes** support inheritance, allowing for more flexible and reusable code.

## Default Constructor:

- **Structs** cannot have an explicit parameterless constructor; they always have an implicit default constructor that initializes all members to their default values.
- **Classes** can have explicit parameterless constructors.

###

### When to Use Structs

1. **Small Data Structures:** Use structs for small, lightweight objects that represent simple data structures. Typically, a struct should not have more than a few fields.
2. **Immutability:** Structs are best suited for immutable types, where the data they hold does not change after creation. Immutable types help avoid unintended side effects and make your code easier to reason about.
3. **Performance Considerations:** Use structs when performance is critical, and you want to avoid the overhead of heap allocation and garbage collection. Since structs are stored on the stack, they can be more efficient for small, short-lived data.
4. **No Need for Polymorphism:** Use structs when you do not need the features of inheritance and polymorphism. Since structs do not support inheritance, they are not suitable if you need to extend or modify behavior through derived types.

## Example of Using a Struct

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

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public override string ToString() => $"({X}, {Y})";
}
class Program
{
    static void Main()
    {
        Point p1 = new Point(10, 20);
        Point p2 = p1;  // p2 is a copy of p1
        p2 = new Point(30, 40);  // This does not affect p1
        Console.WriteLine(p1);  // Output: (10, 20)
        Console.WriteLine(p2);  // Output: (30, 40)
    }
}
```

### When to Use Classes

1. **Complex Data Structures:** Use classes for complex data structures that require methods, properties, and events.
2. **Large Objects:** Classes are better suited for large objects or objects that need to be shared and modified across different parts of your application.
3. **Inheritance and Polymorphism:** Use classes when you need inheritance and polymorphism to create flexible and reusable code.
4. **Managed Lifetime:** Use classes when you need objects with a managed lifetime and garbage collection.

## Example of Using a Class

```cs
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public override string ToString() => $"{FirstName} {LastName}";
}

class Program
{
    static void Main()
    {
        Person person1 = new Person("Alice", "Smith");
        Person person2 = person1;  // person2 references the same object as person1
        person2.FirstName = "Bob";  // This affects person1 as well
        Console.WriteLine(person1);  // Output: Bob Smith
        Console.WriteLine(person2);  // Output: Bob Smith
    }
}
```

In summary, use **structs** for **small**, **simple**, and **immutable data structures** where value semantics are desired. Use classes for more **complex data structures**, where you need **reference semantics**, **inheritance**, and **polymorphism**.


---

Original Source: https://www.mindstick.com/forum/160700/when-to-use-struct-over-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
