C# array equivalent in C++/CLI

Hi,

I'm writing a C++/CLI wrapper for legacy C++ code such that it can be used from e.g. C#. The latter has built-in support for arrays, which even can be jagged.

In order for my C++/CLI wrapper to accept these C# arrays (e.g. as member function parameter), I use the abstract System.Array class from which the C# arrays inherit. Is this "the" approach, or are there better approaches

P.S. While the MSIL code generated by the C# compiler is pretty optimized for e.g. C# double arrays, there is e.g. a boxing/unboxing penalty when using System.Array::SetValue() in the case of a double array as equivalent for the [] operator. Also dealing with C# jagged arrays is not trivial.

Thanks.
Frans.



Answer this question

C# array equivalent in C++/CLI

  • Ben Waldron

    Frans wrote:

    Nishant,

    in the meantime I found that C++/CLI indeed has its own managed array derived from System::Array.

    Do you know how compliant C# arrays and C++/CLI arrays are Can I safely access a C# array through a C++/CLI array reference Any idea of their performance

    Thanks.

    The CLI arrays you create using C# are equivalent to the CLI arrays you create using C++/CLI. Nearly identical MSIL is generated, except for superficial differences.



  • Saresh Naroji

    Thanks !
  • carsc

    Nishant,

    in the meantime I found that C++/CLI indeed has its own managed array derived from System::Array.

    Do you know how compliant C# arrays and C++/CLI arrays are Can I safely access a C# array through a C++/CLI array reference Any idea of their performance

    Thanks.


  • edmond koni

    C# implicitly boxes value types while in C++ you’ll have too explicitly box them with __box. This is nice to know because you can always declare your arrays as Object types which will negate the boxing required but you’ll have to cast the data to use it for something else. So you’re kind of stuck with which is faster or what’s required from your array. You can use one method internally and the other externally, maybe.

    C# can also use the foreach loop, if you want this kind of effect in C++ then you might need to inherit from IEnumerator which has three elements to it, you would use like:

    IEnumerator* ie = myarr->GetEnumerator();
    while(ie->MoveNext())
    Console::WriteLine(ie->Current);

    Here Current actually returns an Object. Although it looks different from C# its exactly the same thing.

    If you want to be able to sort the arrays then inherit from IComparable as well, the nice thing is that you can implement more than one member method for comparing different elements of the array types, weather it’s a name or some other value of the array data type.

    I haven’t used .Net as much as I would but hopefully I’ve given you some information to use.


  • Tanek

    Frans wrote:

    Hi,

    I'm writing a C++/CLI wrapper for legacy C++ code such that it can be used from e.g. C#. The latter has built-in support for arrays, which even can be jagged.

    In order for my C++/CLI wrapper to accept these C# arrays (e.g. as member function parameter), I use the abstract System.Array class from which the C# arrays inherit. Is this "the" approach, or are there better approaches

    P.S. While the MSIL code generated by the C# compiler is pretty optimized for e.g. C# double arrays, there is e.g. a boxing/unboxing penalty when using System.Array::SetValue() in the case of a double array as equivalent for the [] operator. Also dealing with C# jagged arrays is not trivial.

    Thanks.
    Frans.

    See http://www.codeproject.com/managedcpp/cppcliarrays.asp



  • C# array equivalent in C++/CLI