Difference between Structs (Structures) vs Classes in C#
Structs are value types and classes are reference types.
The value type is known as Stack and Reference Type is known as Heap.
The struts are lightweight versions of classes. Structs are value types and can be used to create objects that behave like built-in types.
The structs cannot have a default constructor (a constructor without parameters) or a destructor.
The Structs are a value type and copied on assignment.
The Structs are value types while classes are reference types.
The Structs can be instantiated without using a new operator.
The structs cannot inherit from another structs or class, and it cannot be the base of a class. Every struct inherits directly from System.ValueType, which inherits from System.Object.
A Structs cannot be base class. So, Structs types cannot abstract and are always implicitly sealed.
The Abstract and sealed modifiers are not allowed and structs member cannot be protected or protected internals.
The Function members in structs cannot be abstract or virtual, and the override modifier is allowed only to the override methods inherited from system.ValueType.
A Structs does not allow the instance field declarations to include variable initializers. However, static fields of structs are allowed to include variable initializers.
The structs can implement interfaces.
The structs can be used as a nullable type and can be assigned a null value.
when to use struct or classes?
Classes
Structs
The Assignments of large reference types are cheaper than assignments of large value types.
The Allocations and de-allocations of value types are in general cheaper than allocations and de-allocations of reference types.
Into the classes, two variables can contain the reference of the same object and any operation on one variable can affect another variable.
Into the structs, each variable contains its own copy of the data (except in the case of the ref and out parameter variables), and any operation on one variable does not affect another variable.
The Structs are value types, allocated either on the stack or inline in containing types.
The Classes are reference types, allocated on the heap and garbage-collected.
In this way, struct should be used only when you are sure that -
This logically represents a single value, like primitive types (int, double, etc.).
This is immutable.
This should not be boxed and un-boxed frequently.
Into the all other cases, you should define your types as classes.
struct Location
{ public int a, b;
public Location(int a, int b)
{
this.a= a;
this.b= b;
}
}
Locationa = new Location(20, 20);
Location z = y;
y.a = 100;
System.Console.WriteLine(z.a)
;
Thank You!!! for reading this article.
Hi,
Rahul.
I hope this will definitely help you.
Liked By
Write Answer
Difference between Structs (Structures) vs Classes in C#
Join MindStick Community
You have need login or register for voting of answers or question.
Anonymous User
12-Dec-2018Difference between Structs (Structures) vs Classes in C#
when to use struct or classes?
Into the structs, each variable contains its own copy of the data (except in the case of the ref and out parameter variables), and any operation on one variable does not affect another variable.
In this way, struct should be used only when you are sure that -
This logically represents a single value, like primitive types (int, double, etc.).
This is immutable.
This should not be boxed and un-boxed frequently.
Into the all other cases, you should define your types as classes.
Thank You!!! for reading this article.
Hi,
Rahul.
I hope this will definitely help you.