Hidden Fact about Struct and its Difference with Class

The Hidden Fact about Struct and its Difference with Class

As we know that the basic syntax of STRUCT looks just like a CLASS but there are important differences. I am not going to talk about the differences given in general because you people have already gone through that. I am here, talking about one of its major but hidden difference with CLASS.

As we know that whenever we allocate an instance of a particular type it is always assigned to some default values. With CLASSes all fields are assigned to 0.

But it works slightly different in the case of Value Types-

Every time When we write a STRUCT, C# automatically generates a default parameterless Constructor which initializes all of our storage to 0, so, if we will not want to write any custom constructor for the STRUCT we will not have any problem.

Here, unlike with a CLASS we aren't allowed to replace or override the default constructor. We can however define some extra constructor but default constructor must always be present there and we aren't allowed to write our own.

This is a Short Article which is focusing on Some of the hidden facts about STRUCT and its difference with CLASS. Hope you liked it.