Search code examples
c#c++-climanaged

System:Uint32^ in C++/CLI compiles to VaueType in C#?


In a C++/CLI class library I have the following function:

static System::UInt32^ Fn()
{
    return gcnew System::UInt32(0);
}

When I use the class library in a C# the System::Uint32 is a System::ValueType and the compiler error:

Error 1 Cannot implicitly convert type 'System.ValueType' to 'uint'. An explicit conversion exists (are you missing a cast?)

Why is it that when I'm defiing a System::UInt32 in C++/CLI I get a ValueType in C#?

Thanks


Solution

  • UInt32 is a value type (struct in C#), not a reference type. Therefore, it does not need the ^ handle.

    Switch that method to the following, and it'll appear normally in C#.

    static System::UInt32 Fn()
    {
        return 0;
    }
    

    If you convert the rest of your code to remove the ^ from the value types, you'll find that your C++/CLI code looks more natural, and you'll have less issues interfacing with the .Net Library.