SUMMARY
Classes and structures are very similar. Both are container types that group related variables, methods, and events in a single entity.
Most developers use classes exclusively, primarily because structures are relatively new and developers are more familiar with classes. Structures also cannot take advantage of inheritance.
Another significant factor when picking between classes and structures, however, is their difference in variable type. Classes are reference types, whereas structures are value types. This gives them different behaviors when defining and initializing objects and when passing objects to routines by value and by reference.
When you understand the differences between classes and structures, you can select the one that is more appropriate for your application.
If you build enough classes and structures, you may start to have naming collisions. It is common for developers working on different projects to define similar business classes such as Employee, Customer, Order, and InventoryItem. Although these objects may be similar, they may differ in important details. The Customer class defined for a billing application might include lots of account and billing address information, whereas a repair assignment application might focus on the customer’s equipment and needs.
Having two Customer classes around can result in confusion and programs that cannot easily interact with each other. Namespaces can help categorize code and differentiate among classes. You can ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access