> Is there any point in using a struct if the struct contains a string,
> given that a string is a reference type?

If you are going to embed a reference type within a value type, and you
expect the Equals method to be called a lot (which especially occurs when
you place the struct is collections that are sorted), make sure you override
the Equals method.  The implementation of ValueType.Equals will use a
bitwise comparison between two values if it can, but if a reference type is
involved, a bitwise comparison is not sufficient.  This is because the value
types may contain different pointers to reference types, but if the
reference types override their Equals methods to provide value-based
equality (as opposed to the default reference-based equality inherited from
System.Object), two value types that do not have binary equality are still
Equal.

The upshot of this is that if ValueType.Equals will use reflection if the
value type contains a reference type to compare each member variable.  This
is a massive hit - something like 2 orders of magnitude compared to doing it
yourself.

If you are going to be using the type is a collection most of the time, a
reference type will be quicker because it avoids the boxing hit.

Nick

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to