Class vs Structure

Here is a list of the differences between the two.

The struct is value type in C# and it inherits from System.ValueType

The class is reference type in C# and it inherits from the System.Object Type

The struct value will be stored on the stack memory.

The class object is stored on the heap memory. The object will be under garbage collection and automatically removed when there is no reference to the created objects.

The struct use the array type and its good to use for read only and light weight object.

The class uses the collection object type and it can perform all the operations and designed for complex data type storage.

The struct can't be base type to the classes and also to the other structure.

The class can inherit another class, interface and it can be base class to another class.

The struct can only inherit the interfaces

The class can inherit the interfaces, abstract classes.

The struct can have only constructor.

The class can have the constructor and destructor.

The struct can instantiated without using the new keyword.

The new keyword should be used to create the object for the class

The struct can't have the default constructor

The class will have the default constructor

The struct is by default sealed class hence it will not allow to inherit. It can't use the abstract, sealed, base keyword.

The class can be declared as abstract, sealed class

The struct can't use the protected or protected internal modifier.

The class can use all the access modifiers.

The struct can't initialize at the time of declaration.

The class can have the initializes fields.

Ebook Download
View all
Learn
View all