Sunday, May 17, 2009

What is the difference between classes and structs in Microsoft.Net?

• A struct is a value type, while a class is a reference type.
• When we instantiate a class, memory will be allocated on the heap. When struct gets initiated, it gets memory on the stack.
• Classes can have explicit parameter less constructors. But structs cannot have this.
• Classes support inheritance. But there is no inheritance for structs. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Like classes, structures can implement interfaces.
• We can assign null variable to class. But we cannot assign null to a struct variable, since structs are value type.
• We can declare a destructor in class but can not in struct.

No comments:

Post a Comment