> At work, our development team has a development standards document that
insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make the change. I
know that Structures are value types, sit on the stack, and are
generally more efficient to manipulate than reference types (i.e.
Classes). Structures cannot use inheritance, the finalize method or
default constructors. Can anyone think of a situation where using
Structures is more suitable than using Classes?
1. MS has about 100 structures in .net. What is good enough for MS is good
enough for your team. If the team truly has an aversion to structures, then
they should avoid using them as well as defining them. That means don't use
point, size, rectangle, color, etc. It also means dont use DictionaryEntry,
so no hashtables either. And so on. Now I believe that your team's stance
"Structures should never be used" means they should not be created, but that
you can use the MS provided structures. Nevertheless, you should use this
thought to make the team see that structures are not absolutely evil, and
thus an absolute prohibition is unwarranted.
2. I have a structure consisting of 4 bytes and an integer, and I've
arranged for it to occupy 8 bytes of storage. No can do with a class, and if
I have enough of them, memory usage will be impacted. Maybe you will need to
interop with pre-.net functionality. With structures, you can generally
mimic old style C structs. In other words, a structure is ideal for a data
layout that you have to work with (ie a data layout that was thrust upon you
rather than one you design yourself).
3. My general usage guide for a structure is (1) it aggregates a small
number of value types and strings, and (2) a structure variable's scope is
that of a local variable in a procedure. In other words, they are simple and
short lived. They behave like an integer, but maybe you need two integers.
This is what value types are all about, and your post indicates that you
understand. To not use them is like not using integers. This argument is
really a philosophical stance against the position of your team. If the team
won't embrace structures, maybe you could arrange for a team committee to
approve structures. My guess is that a very small number of structures would
pass the committee, and that is probably all you need.