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

Asked 18-Dec-2017
Viewed 495 times

0

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


1 Answer


0

Difference between Struct and Class: 
The main difference between Struct and Class is, Struct is value type but the Class is referenced type. So Struct store in the stack, but Class object store in heap. The Detailed difference between Struct and Class are as follows:

Struct 
Class
The Struct is usually used for the smaller amount of data. Class is used for a large amount of data.
The Struct cannot be inherited to other types. The Class can inherit the interfaces, abstract classes.
A struct does not have permission to create any default constructor. Class will have the default constructor.
The struct is the value type and it inherits from System.ValueType. But the Class in C# is a reference type and it inherits from the System.Object Type.
Its no need to create an object by new keyword. We can’t use an object of a class with using the new keyword.
The struct can have a constructor.But the Class can have Constructor and Destructor.
The Struct can’t contain a volatile field.The Class contains a volatile field.
Struct variable can’t be initialized with a null value.

But you can be initialized Class variable with a Null value.