Differences between Classes and Structures in C#

 

Class is a reference type and Structure is a value type. 

For Class memory allocation is performed on managed heap, so automatic memory management is available with garbage collector for Classes. Whereas for Structures, memory allocation is performed on stack, so faster i access but doesn't get the advantage of automatic memory management. 

Classes are used for representing huge volumes of data whereas Structures are used for representing the small volumes of data. 

Requires the new keyword to create the object for classes is compulsory. Requiring the new keyword for creating object Structures is only optional. 

Classes can also be initialized under constructor as well as we can assign a value referring the variable with its object. Structures can be initialized only under constructor or can be assigned with a value referring the variable with object. 

Classes can contain any constructor(zero argument/parameterized) using which we can create the object. Structures requires a zero argument constructor if at all we want to create object of structure without new keyword. So there is a implicit default constructor available to a structure. 

Programmers can define either zero argument or parameterized constructors for classes. Programmers can define only define parameterized constructors because there is an implicit default constructor for structures. 

Classes supports both interface and inheritance implementation. Structures supports only interface inheritance but not inheritance implementation i.e., a structure cannot be inherited from another structure. 

All predefined data types that come under reference type(classes) category(string & object) are implemented internally as classes under the System namespace. All predefined data types comes under value type(structures) category int, float, char...etc are implemented as structures internally under system namespace like int32, single, namespace...etc.