Search code examples
c#c++type-conversionvariant

VARIANT datatype of C++ into C#


What is equivalent of the VARIANT datatype of C++ in C#?

I have code in C++ which uses the VARIANT datatype. How can I convert that code in C#?


Solution

  • This is a tricky question.

    From C# 4, you can use dynamic to indicate that the type is known at run-time.

    By my personal understanding, however, c++ requires the type known at compile time. Thus you might consider to use object, but object in C# is an existent type.

    For the concept of multi-type, single value (AKA polymorphism) of VARIANT, you would not need to find a corresponding type in C#, just define your classes and interfaces. You can always reference an object as its interface which the class implements.

    If you are porting the code, and to figure out a syntax that you can simply use in LHS and for the considering of the type is known at compile time, then use var.