Pointers to a typed variable in C#: Interfase, Generic, object or Class? (Boxing/Unboxing)
Posted
by
PaulG
on Stack Overflow
See other posts from Stack Overflow
or by PaulG
Published on 2011-01-06T00:18:41Z
Indexed on
2011/01/06
0:53 UTC
Read the original article
Hit count: 152
First of all, I apologize if this has been asked a thousand times. I read my C# book, I googled it, but I can't seem to find the answer I am looking for, or I am missing the point big time.
I am very confused with the whole boxing/unboxing issue. Say I have fields of different classes, all returning typed variables (e.g. 'double') and I would like to have a variable point to any of these fields. In plain old C I would do something like:
double * newVar;
newVar = &oldVar;
newVar = &anotherVar;
...
In C#, it seems I could do an interfase, but would require that all fields be properties and named the same. Breaks apart when one of the properties doesn't have the same name or is not a property.
I could also create a generic class returning double, but seems a bit absurd to create a class to represent a 'double', when a 'double' class already exists. If I am not mistaken, it doesn't even need to be generic, could be a simple class returning double.
I could create an object and box the typed variable to the newly created object, but then I would have to cast every time I use it.
Of course, I always have the unsafe option... but afraid of getting to unknown memory space, divide by zero and bring an end to this world.
None of these seem to be the same as the old simple 'double * variable'. Am I missing something here?
© Stack Overflow or respective owner