Struct vs Class in C#

--

What is the difference between struct and class? Class is havier than a struct and also the class is reference type object, but struct is a value type.

On the other hand, struct comes from System.ValueType that comes from System.Object and it cant inherit from another struct or class.

Struct doesn't have a constructor method or destructor. We can use struct without new operator.

Struct cant is a base, it is always implicitly sealed.(sealed modifier prevents other classes from inheriting from it)

Abstract and sealed modifiers are not suitable for a struct.

A struct function cant is abstract or virtual but an override modifier is allowed only if the method gets an inheritance from System.ValueType

You can implement the interface with a struct

Note: with c# 11 you can create a constructor method for structs.

--

--

Çağlar Can SARIKAYA
Çağlar Can SARIKAYA

No responses yet