What is the Difference Between Class and Struct in C#?

πŸ’‘ Concept: Class vs Struct

In C#, both classes and structs are used to define custom data types, but they differ in memory behavior, inheritance, and usage scenarios. Understanding these differences helps choose the right type for a given situation.

πŸ“˜ Quick Intro

A class is a reference type stored on the heap and supports inheritance. A struct is a value type stored on the stack and is ideal for small, immutable objects. Classes support more complex behaviors like polymorphism.

🧠 Analogy

Imagine a class as a shared apartment buildingβ€”you give people the address, and they all point to the same place (reference). A struct is like a personal tentβ€”everyone has their own separate tent, even if the setup is similar (value).

πŸ”§ Technical Explanation

  • Classes are reference types; structs are value types.
  • Classes are stored on the heap; structs on the stack (usually).
  • Classes support inheritance and polymorphism; structs do not.
  • Structs are better for lightweight, immutable types with short lifespan.
  • Modifying a class instance affects all references; modifying a struct affects only that copy.

🎯 Use Cases

  • βœ… Use classes when you need inheritance, polymorphism, or reference sharing.
  • βœ… Use structs when performance matters and your object is small and immutable.
  • βœ… Structs are ideal for value-like types such as coordinates, colors, measurements.
  • βœ… Classes are more suited for domain models, services, and complex behaviors.

πŸ’» Code Example

public class Person
{
    public string Name;
    public void Greet() => Console.WriteLine($""Hello, I am {Name}"");
}

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

// Usage
var p1 = new Person { Name = ""Alice"" };
var p2 = p1;
p2.Name = ""Bob"";
Console.WriteLine(p1.Name); // Bob β€” same reference

var c1 = new Coordinate(10, 20);
var c2 = c1;
c2.X = 99;
Console.WriteLine(c1.X); // 10 β€” copied by value

❓ Interview Q&A

Q1: What is the primary difference between class and struct?
A: Class is a reference type, struct is a value type.

Q2: Can structs inherit from classes?
A: No, structs cannot inherit from any type except interfaces.

Q3: Where is a struct stored?
A: On the stack (typically).

Q4: Do structs support polymorphism?
A: No, they don’t support polymorphic behavior.

Q5: Can classes be passed by value?
A: No, classes are always passed by reference.

Q6: Which is faster in terms of memory allocation?
A: Structs are faster for small, frequent allocations.

Q7: Are structs good for mutable objects?
A: No, they're best used for immutable data.

Q8: Can structs have methods?
A: Yes, they can have methods and properties.

Q9: What happens when you assign one class object to another?
A: Both point to the same object.

Q10: Do structs support parameterless constructors?
A: No custom parameterless constructors allowed.

πŸ“ MCQs

Q1. What is the default behavior of struct assignment?

  • Shared reference
  • Copied by value
  • Heap reference
  • Inheritance

Q2. Which of these supports inheritance?

  • Struct
  • Class
  • Enum
  • All of the above

Q3. Where is a class stored?

  • Stack
  • Heap
  • Register
  • Thread

Q4. Can a struct be null?

  • Yes
  • No
  • Sometimes
  • Only when static

Q5. Which supports polymorphism?

  • Struct
  • Class
  • Static
  • Array

Q6. Can struct fields be initialized inline?

  • Yes
  • No (before C# 10)
  • Only in methods
  • Only with readonly

Q7. Is struct a value or reference type?

  • Reference type
  • Value type
  • Dynamic type
  • Generic type

Q8. Can class methods be virtual?

  • No
  • Yes
  • Only abstract
  • Only static

Q9. Which is more suitable for large objects?

  • Class
  • Struct
  • Enum
  • Interface

Q10. Can structs implement interfaces?

  • Yes
  • No
  • Only IDisposable
  • Only if abstract

πŸ’‘ Bonus Insight

Structs avoid GC pressure and are ideal in high-performance scenarios like graphics or games. However, always consider behavior and copying semantics before choosing struct over class.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: