Structures vs Classes in .NET


Just like C and C++, C# allows structures.  However, structures in C# can do a lot more than in C or C++.  C# structures can have constructors, properties, private members, methods, and more.  In fact, it would seem that structures in C# can do everything that classes can do.

The question then arises “what is the difference between a class and a structure ?

To be pedantic, the answer is “just the syntax” – the real difference is between an instantiated class (an object) and an instantiated structure (a struct).

  • objects are always passed by reference.
  • structs are always passed by value.

That’s it!  Pass a struct as a parameter to a method and the method will be working on a copy of the struct.   Any changes to the struct’s values made by the method will not be reflected in the original struct (unless the struct is passed as a ref parameter, of course).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s