Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between Class and Structure in C#
In C#, both classes and structures are used to define data types and hold data. While they appear similar in functionality, they have fundamental differences in how they store and manage data. Classes are reference types that provide more flexibility and advanced features, while structures are value types designed for simple data storage with better performance in certain scenarios.
Understanding these differences is crucial for choosing the right type for your specific use case and writing efficient C# code.
Syntax
Following is the syntax for declaring a class −
public class ClassName {
// fields, properties, methods
public int field;
public ClassName() { } // constructor allowed
}
Following is the syntax for declaring a structure −
public struct StructName {
// fields, properties, methods
public int field;
// parameterless constructor not allowed
}
Key Differences Between Class and Structure
| Aspect | Class | Structure |
|---|---|---|
| Data Type | Reference type - stored as reference with memory address | Value type - stored directly in memory location |
| Memory Storage | Allocated on heap memory | Allocated on stack memory |
| Constructor | Can have custom parameterless and parameterized constructors | Cannot have custom parameterless constructor, but can have parameterized constructors |
| Instantiation | Requires new keyword for instantiation |
Can be instantiated without new keyword |
| Inheritance | Supports inheritance from classes and interfaces | Cannot inherit from classes, but can implement interfaces |
| Access Modifiers | Supports all access modifiers including protected
|
Does not support protected modifier |
| Assignment Behavior | Assigns reference - multiple variables point to same object | Assigns value - creates independent copies |
Example - Class Implementation
using System;
public class Person {
public string name;
public int age;
public Person() {
name = "Unknown";
age = 0;
}
public Person(string n, int a) {
name = n;
age = a;
}
public void DisplayInfo() {
Console.WriteLine($"Name: {name}, Age: {age}");
}
}
public class Program {
public static void Main() {
Person p1 = new Person("Alice", 25);
Person p2 = p1; // Reference assignment
p1.DisplayInfo();
p2.name = "Bob";
p1.DisplayInfo(); // Shows "Bob" - same object referenced
}
}
The output of the above code is −
Name: Alice, Age: 25 Name: Bob, Age: 25
Example - Structure Implementation
using System;
public struct Point {
public int x;
public int y;
public Point(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;
}
public void DisplayPoint() {
Console.WriteLine($"Point: ({x}, {y})");
}
}
public class Program {
public static void Main() {
Point p1 = new Point(10, 20);
Point p2 = p1; // Value assignment - creates copy
p1.DisplayPoint();
p2.x = 30;
p1.DisplayPoint(); // Shows original values - independent copy
p2.DisplayPoint();
}
}
The output of the above code is −
Point: (10, 20) Point: (10, 20) Point: (30, 20)
Memory Allocation Comparison
When to Use Classes vs Structures
Use Classes when:
You need inheritance or polymorphism
The data size is large (> 16 bytes recommended)
You need reference semantics (multiple variables pointing to same data)
You need advanced features like destructors or protected members
Use Structures when:
Representing simple data with small size (< 16 bytes recommended)
You want value semantics (independent copies)
Performance is critical (faster allocation/deallocation)
Immutable data representation is preferred
Conclusion
Classes and structures serve different purposes in C#. Classes are reference types stored on the heap, offering inheritance and advanced features, while structures are value types stored on the stack, providing better performance for simple data. Choose classes for complex objects requiring inheritance, and structures for simple, small data types requiring value semantics.
