Since it's a value type its data is
A Structure behaves in many ways like a Class. It encapsulates data and
code, and it can expose public data, methods, and properties. However, data
for a Structure is allocated on the stack. If you allocate a local
Structure variable then its data resides on the stack until it goes out of
scope, at which time its stack space is simply reclamed. On the other hand,
a class is allocated from the managed heap. When its life is over then the
GC needs to run to clean it up.
In my case, I have a routine that executes frequently. That routine wants
to manipulate bits in an Integer. I could do this:
Sub ExecuteThis10TimesPerSecond()
Dim myBits As New BitArray(m_value)
...
End Sub
The problem with this is that every time my routine runs I create another
instance of the BitArray class which needs to be returned to the heap by the
garbage collector. But by writing my own "lightweight" bit manipulation
Structure rather than a Class then the above code no longer allocates memory
from the managed heap.
Well,, it's not possible because VB needs specific types to do this. These
are basic operations on the native data types that are compiled inline and
therefore the compiler exactly need to know what it is dealing with. It
needs to know the size, the storage format of the value. For example,
performing an And operation on Integer values must create different code
and only access 32 bits. Without the compiler knowing the type it can not
know which code to generate.
I actually knew that from the start. My real reason for posting the
question was to see if anyone had some brilliant (or sneaky) way around the
fact that generics don't allow you to constrain their type to either numeric
value types or to types that implement a particular operator. The answer
will probably be "no, you can't get there from here", but it doesn't hurt to
ask.