---
title: "What are the differences between Class and Struct in C#?"  
description: "What are the differences between Class and Struct in C#?"  
author: "Rahul Roi"  
published: 2020-11-16  
updated: 2023-06-06  
canonical: https://www.mindstick.com/interview/33756/what-are-the-differences-between-class-and-struct-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# What are the differences between Class and Struct in C#?

#### Differences between Class and Struct in C#

| Class \ | Struct \ |
| --- | --- |
| Supports Inheritance \ | Does not support Inheritance \ |
| Class is Pass by reference (reference type) \ | The struct is Pass by Copy (Value type) \ |
| Members are private by default \ | Members are public by default \ |
| Good for larger complex objects \ | Good for Small isolated models \ |
| Can use waste collector for memory management \ | Cannot use Garbage collector and hence no Memory management \ |

## Answers

### Answer by Aryan Kumar

In C#, classes and structs are both data types that can be used to create objects. However, there are some key differences between the two.

## Classes

- Classes are reference types. This means that when a class object is created, a reference to the object is stored on the heap.
- Classes can be inherited from other classes. This allows you to create new classes that share the properties and methods of existing classes.
- Classes can be virtual. This allows you to create objects that can be used polymorphically.
- Classes can be sealed. This prevents other classes from inheriting from them.

## Structs

- Structs are value types. This means that when a struct object is created, the actual data for the object is stored on the stack.
- Structs cannot be inherited from other structs.
- Structs cannot be virtual.
- Structs cannot be sealed.

Here is a table that summarizes the key differences between classes and structs:

| Feature | Class | Struct |
| --- | --- | --- |
| Type | Reference type | Value type |
| Storage | Heap | Stack |
| Inheritance | Can be inherited | Cannot be inherited |
| Polymorphism | Can be virtual | Cannot be virtual |
| Sealing | Can be sealed | Cannot be sealed |

## When to use classes

Classes are a good choice for data types that need to be inherited from other classes, or that need to be used polymorphically. For example, you might use a class to represent a user, or an animal.

## When to use structs

Structs are a good choice for data types that are small and simple, or that need to be passed around by value. For example, you might use a struct to represent a point, or a color.

Ultimately, the best type of data type to use will depend on the specific requirements of the situation.

### Answer by Rahul Roi

#### Differences between Class and Struct in C#

| Class \ | Struct \ |
| --- | --- |
| Supports Inheritance \ | Does not support Inheritance \ |
| Class is Pass by reference (reference type) \ | The struct is Pass by Copy (Value type) \ |
| Members are private by default \ | Members are public by default \ |
| Good for larger complex objects \ | Good for Small isolated models \ |
| Can use waste collector for memory management \ | Cannot use Garbage collector and hence no Memory management \ |


---

Original Source: https://www.mindstick.com/interview/33756/what-are-the-differences-between-class-and-struct-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
