Search code examples
c#clrmanaged-c++variant

How can I implement variant types in the CLR/Managed C++?


In the .net CLR Object is the base for all class objects, but not basic types (e.g. int, float etc). How can I use basic types like Object? I.e. Like Boost.Variant?

E.g. like :-

object intValue( int(27) );
if (intValue is Int32)
    ...

object varArray[3];
varArray[0] = float(3.141593);
varArray[1] = int(-1005);
varArray[2] = string("String");

Solution

  • object, via boxing, is the effective (root) base-class of all .NET types. That should work fine - you just need to use is or GetType() to check the types...

    object[] varArray = new object[3];
    varArray[0] = 3.141593F;
    varArray[1] = -1005;
    varArray[2] = "String";