Differences between struct and class

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
puzzlecracker said:
what are the most usable, prominent, and must-to-know ones?

From a previous post you seem to be thinking of the C# type system in
terms of C++. You cannot do this, often the primary differences between
languages are their types systems. For instance C uses a "tag" system,
C++ uses a "type" system where everything is a "type" i.e. a class (well
except fundamental types, anyways, we're not talking C++).

In C# there is are two fundamental types Value types (structs) and
Reference types (classes). This effects, primarily their equality and
assignment semantics, and thus the way they are passed as parameters to
methods. These semantics permeate everything, it is not like C++ where
parameters are passed by value by default but can be pass by reference
using the reference operator (&). There is no such construct on C#,
value types are value types and reference types are reference types,
forever. Well not quite, you can "box" a Value type into a Reference
type, something that should generally be avoided.
 
puzzlecracker said:
what are the most usable, prominent, and must-to-know ones?

The most fundamental is that a class is a reference type and a struct is
a value type.

To work well, a struct should be small and immutable. The members of a
struct are generally value types (with a possible exception for strings,
as they are also immutable).

If you don't have any special reason to use a struct, stick to creating
classes.
 
Back
Top