Structure and Class in VB.NET

HTML clipboard

This blog that deals with types introduced the class and structure, the class and structure concepts are consider attributes, member variables, member functions, constructors, and destructors. Even with that background information, people often confuse the concepts of class and structure. However, each keyword has distinct uses.

A class is a data structure that contains data members (e.g., constants and fields), function members (e.g., methods, properties, indexers, events, operators, instance constructors, static constructors, and destructors), and nested types. Class types support inheritance.

Structure data types are similar to classes: they represent data structures that can contain data members and function members. Unlike classes, structures are value types and do not require heap allocation. They are always created on the stack. A variable of a type structure directly contains the data of the structure, whereas a variable of a type class contains a reference to the data.

Structure data types are particularly useful for small data structures that have value semantics. Complex numbers, points in a coordinate system, or key-value pairs in a dictionary are all good examples of structures. Structures should have few data members. They do not require use of inheritance or referential identity, and they can be conveniently implemented using value semantics where assignment copies the value instead of the reference.

The simple types provided by C#, such as integer, double, and Boolean, are in fact all structure types. Just as these predefined types are structures, so is it possible to use structures and operator overloading to implement new "primitive" types in the VB.NET.

Conclusion

Hope this blog would have helped you in understanding difference in structure and class in VB.NET