1.

What is the difference between a struct and a class in C#?

Answer»
StructClass
Structs are value-type entities stored in stack memory.Class is a reference type entity stored in heap memory.
Allocation and de-allocation of Structs are cheaper when the size of the Structure is small.Allocation and de-allocation of Classes are costlier when the size of the class is small.
Allocation and de-allocation of Structs are costlier as compared to classes when the size of a struct is large.Allocation and de-allocation of Classes are cheaper as compared to Structs when the size of the class is large.
Structs can contain limited kind of entities like variables, structs, etc, and hence is used to create smaller codes.Classes can contain all kind of entities like variables, functions, constructors, structs, etc, and hence is used to create large codes efficiently.
Structures are not allowed to contain parameterless constructors or destructors, but only parameterized constructors and static constructors.Classes can contain any kind of constructors or destructors.
An instance of a struct can be created by using the new keyword, as well as without using a new keyword.To create an instance of a class, a new keyword is used.
A Struct cannot implement Inheritance and hence cannot inherit another struct or class.A Class can implement Inheritance and can inherit from another class.
The attributes of a struct cannot be specified as protected.The attributes of a class can be specified as protected.
A struct can neither have a virtual method nor an abstract method.A class can have virtual methods as well as abstract methods.



Discussion

No Comment Found