Mattias Sjögren wrote:
Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.
Not much of a restriction in practice, is it? What good is an empty
struct anyway? If you only have static members you may as well us a
static class.
The CLR requires value types to either have instance fields or an
explicit size so for empty structs the C# compiler must effectively
add [StructLayout(..., Size=1)]. So now you have a type that appears
to be empty but still actually takes up one byte. I'm not sure I like
that better than what the VB compiler is doing.
Mattias
Truly not a restriction in practice.
I can not come up with a single use for an empty structure. A value type
that doesn't carry it's own value is totally useless.
The closest thing to something useful I can think of is creating
different empty structures that would override the ToString method and
return different values. Then you could put instances of the different
structs in something like an ArrayList, and then you could use the
ToString method on the objects in the ArrayList without needing to know
what structure it really was, and they would return different values.
But then again you could just put the strings in the ArrayList and it
would have exactly the same effect...