Differences between Class and Structure


1. Classes are Reference types and Structures are Values types.
2. Classes will support an Inheritance whereas Structures won’t.
3. Classes can have explicitly parameterless constructors whereas structures can’t.
4. in structure, you can not declare destructor but in class it is possible.
5. The “this” keyword gives different meaning in structure and class. How?
-> In class, “this” keyword is classified as value type of class type within which it is used like inside instance constructor or instance method.
-> In structure, “this” keyword is classified as variable type of structure type within which it is used.
======================================================
Why struct doesn't support inheritance ?
---------------------------------------
If you want that class should not be inherited in your design, you can use class as sealed. But sealed class can inherit from interface. If you think virtual method cannot to be inherited in derived class at one stage, we can declare a method with sealed+override combination.
By default structures are sealed, that is the reason structures are not supporting inheritance.

Comments

Popular posts from this blog

Explain what is routing in MVC? What are the three segments for routing important?

What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?

Explain what is the difference between View and Partial View?