C#  

Difference between Class and Structure in C#

Title image

In C#, both classes and structures (structs) are powerful tools for defining custom types, but they behave quite differently under the hood. Understanding their differences is crucial for writing efficient, maintainable, and performant code.

🔍 Reference vs Value Types

At the core, the most fundamental difference is how memory is allocated:

  • Classes are reference types. Instances are allocated on the heap, and variables hold references (pointers) to those instances.
  • Structs are value types. They are usually stored on the stack or inline in containing types, and are copied by value, meaning each copy is independent.

💡 Example: Reference vs Value Semantics

// Class: Reference type
public class Person
{
    public string Name;
    public int Age;
}

Person p1 = new Person { Name = "Alice", Age = 30 };
Person p2 = p1;
p2.Age = 31;
Console.WriteLine(p1.Age); 

// Outputs 31 — same object
// Struct: Value type
public struct Point
{
    public int X;
    public int Y;
}

Point a = new Point { X = 10, Y = 20 };
Point b = a;
b.X = 99;
Console.WriteLine(a.X); 

// Outputs 10 — independent copy

⚖️ Comparison Table: Class vs Struct

Feature Class (Reference Type) Struct (Value Type)
Memory Allocation Heap Stack or inline
Type Semantics Passed by reference Passed by value
Inheritance Supports single inheritance Does not support inheritance (sealed by default)
Constructors Supports default, parameterized, and static Only parameterized and static constructors
Finalizer (Destructor) Supported Not supported
Polymorphism Virtual/abstract methods allowed Not supported
Protection Modifiers Supports protected members No support for protected in structs
Usage Large, complex, mutable, polymorphic types Small, lightweight, immutable types
Instance Creation Requires new new optional
Performance May incur GC overhead; better for large objects Faster to copy; better for short-lived, small data

🛠️ Use Cases: When to Use Structs vs Classes

✅ Use a Struct When:

  • The data type is small (generally ≤16 bytes)
  • It is immutable
  • You don’t need inheritance or polymorphism
  • You want performance optimization and reduced memory pressure (avoiding heap allocations)
  • Examples: Point, Vector2, DateTime, Color, MoneyAmount

✅ Use a Class When:

  • You need to inherit or extend behavior
  • You are dealing with large or mutable objects
  • Reference sharing is necessary (e.g., two variables referencing the same object)
  • You require finalizers, polymorphism, or interfaces
  • Examples: Employee, Customer, Stream, Controller, DbContext

🔄 Behavior in Practice

Let’s look at another class and struct example to visualize real-world usage:

🧑‍🏫 Class Example: Author

public class Author
{
    public string Name;
    public string Language;
    public int ArticleCount;
    public int RevisionCount;

    public void ShowDetails()
    {
        Console.WriteLine($"Author: {Name}\nLanguage: {Language}\nArticles: {ArticleCount}\nRevisions: {RevisionCount}");
    }

    public static void Main()
    {
        Author author = new Author
        {
            Name = "Ankita",
            Language = "C#",
            ArticleCount = 80,
            RevisionCount = 50
        };

        author.ShowDetails();
    }
}

🚗 Struct Example: Car

public struct Car
{
    public string Brand;
    public string Model;
    public string Color;
}

class Program
{
    static void Main()
    {
        Car car;
        car.Brand = "Bugatti";
        car.Model = "Veyron EB 16.4";
        car.Color = "Gray";

        Console.WriteLine($"Brand: {car.Brand}\nModel: {car.Model}\nColor: {car.Color}");
    }
}

🚀 Performance Considerations

  • Structs avoid garbage collection if used wisely, making them faster in high-performance, short-lived scenarios (like game development or numeric processing).
  • However, large structs (>16 bytes) can degrade performance due to copying cost.
  • Misusing structs (e.g., making them mutable or using them for complex types) can lead to hard-to-find bugs.

✅ Best Practices

  • Use structs only for simple, short-lived, immutable data.
  • Keep struct size small to avoid expensive copies.
  • Avoid complex behavior or polymorphism in structs.
  • Use classes if you need inheritance, shared state, or large objects.

📌 Summary

Choose Struct for: Choose Class for:
Lightweight, immutable data Complex, mutable objects
High-performance, low-allocation scenarios Scenarios requiring polymorphism
Small objects like points, colors, and records Objects with long lifetimes or shared state

Understanding when and why to choose between class and struct can help you write more efficient, maintainable, and cleaner C# code. Pick wisely based on your use case, and you’ll avoid performance pitfalls and unnecessary complexity.