LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Struct vs Class in C#

Both help us create custom types, but they behave differently when assigned, passed to methods or stored in collections. Choosing the right one helps us write efficient and maintainable applications.

1Struct vs Class in C#

Hi friends, in this article we will learn the difference between struct and class in C#. Both help us create custom types, but they behave differently when assigned, passed to methods or stored in collections. Choosing the right one helps us write efficient and maintainable applications.

2Think About a Shopping Cart

Imagine a shopping cart storing a product's dimensions, such as height and width. These small values can be grouped into a struct.

Now imagine a complete customer account with name, address and order history. A class is a better choice because it represents an entity with identity and shared state.

3How Struct and Class Differ

1. Struct is a value type

Assigning a struct copies its data. Changes to the copied variable do not change the original.

2. Class is a reference type

Assigning a class variable copies its reference. Both variables can point to the same object.

3. Inheritance support

Classes support class inheritance. Structs cannot inherit from another struct or class, but can implement interfaces.

4. When should we use them?

Use structs for small value-like data. Use classes for objects with identity, shared state or complex behaviour.

Feature Struct Class
Type Value Reference
Assignment Copies data Copies reference
Inheritance No class inheritance Supported
Null by default No, unless nullable Yes
Remember Structs are not always stored on the stack. Their storage depends on where and how they are used.

4Let's Compare Them with Code

We will create a struct and a class with the same property. Then we will copy each and update the copied value.

Program.csC#
using System;

struct ProductValue
{
    public int Price { get; set; }
}

class ProductReference
{
    public int Price { get; set; }
}

class Program
{
    static void Main()
    {
        var firstValue = new ProductValue { Price = 100 };
        var secondValue = firstValue;
        secondValue.Price = 200;

        Console.WriteLine(firstValue.Price);
        Console.WriteLine(secondValue.Price);

        var firstProduct = new ProductReference { Price = 100 };
        var secondProduct = firstProduct;
        secondProduct.Price = 200;

        Console.WriteLine(firstProduct.Price);
        Console.WriteLine(secondProduct.Price);
    }
}
Pro Tip Prefer readonly struct for small immutable value objects. It helps prevent accidental changes and can improve performance in suitable scenarios.

5Read the Program Result

The struct copy has its own value. Both class variables refer to the same object, so both display 200.

Output
100
200
200
200
Common Mistake Do not assume every struct is automatically faster than a class. Consider size, copying, allocation and usage before choosing.

6Watch the Difference as Code Runs

Program.cs - Visual Studio Code
● Program.cs
▤⌕⑂⚙
 
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
 
Ln 1, Col 1  UTF-8  C#

7Quick Revision

Structs are value types, while classes are reference types. Struct assignment copies data, and class assignment copies a reference. Select the type based on the behaviour and purpose of your data.

  • Struct: independent value copies.
  • Class: shared object references.
  • Structs can implement interfaces.

8Interview Questions to Practise

Q1Easy

What is the main difference between struct and class?

Answer Struct is a value type. Class is a reference type. Their assignment and copying behaviour is different.

Q2Easy

Can a struct inherit from a class?

Answer No. A struct cannot inherit from a class or another struct, but it can implement interfaces.

Q3Medium

Can a struct contain a constructor?

Answer Yes. Modern C# supports instance constructors in structs. Structs also have a default initialization behaviour.

Q4Medium

Can a struct be nullable?

Answer Yes. Use Nullable<T> or the shorthand syntax, such as int? or MyStruct?.

Q5Hard

When should we choose a struct over a class?

Answer Choose a struct for small, value-like data where independent copies are expected. Choose a class for identity and shared mutable state.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.