Difference between Class and Structure in C#

CsharpServer Side ProgrammingProgramming

In order to differentiate between Class and Structure, we have to first understand that both structure and class seems to be equivalent in the context of holding and defining the data. Both of these could define as well as hold some default values in their data members. But if we consider them beyond this context then Class provides more flexibility along with functionality as compared to the Structure.

Following are the important differences between Class and Structure.

Sr. No.KeyClassStructure
1Data TypeData defined in a class is stored in the memory as a reference and has particular address in order to get accessed, so it could be stated as Classes are of Reference type.On other hand in case of Structure the value of data member gets stored directly either in the stack or inlined containing types so Structures are considered as of Value type.
2StorageAs mentioned in above point, the data members are stored as reference in case of Class and reference types are allocated on heap memory.On other hand in Structure direct value get to be stored so the value types are allocated on stack memory.
3ConstructorClasses can be defined with constructor as well as de-constructor as it has their functionality implemented.On other hand Structure could not have either Constructor or De-constructor as no such implementation is provided in it.
4InstantiateAs Class introduced with constructor with the help of which Class can instantiate its instance with new keyword.On other hand Structure can create an instance without new keyword.
5InheritanceClass can inherit properties from its parent class as it supports the Inheritance feature.On other hand Structure does not support the Inheritance.
6Protected ModifierWe can use protected modifier with the data members defined in the class as Class supports protected modifier.On other hand Structure does not support the protected modifier.
Published on 09-Jun-2020 12:06:45