2

In the C++/CLI project I have the method void DoSomething(long x);. If I want to use it in any unit-test written in C#, the method parameter x shows up as type int.

Why do I have to change the signature to void DoSomething(long long x); to use it with parameters of type long in my unit-tests (C#)?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Christian St.
  • 1,751
  • 2
  • 22
  • 41

2 Answers2

10

long is a keyword both in C# and C++. They simply don't mean the same thing. The C++/CLI designers went for the C++ interpretation since C++ was the target interop language.

Not exactly the only unintuitive mapping:

  • C# byte => C++/CLI unsigned char
  • C# sbyte => C++/CLI char
  • C# char => C++/CLI wchar_t
  • C# ushort => C++/CLI unsigned short
  • C# uint => C++/CLI unsigned int
  • C# long => C++/CLI long long
  • C# ulong => C++/CLI unsigned long long
  • C# string => no equivalent, use System::String^
  • C# decimal => no equivalent, use System::Decimal
  • C# object => no equivalent, use System::Object^
  • C# enum => C++/CLI public enum class
  • C# struct => C++/CLI value struct
  • C# class => C++/CLI ref class
  • C# interface => C++/CLI interface class
  • C# nullable types with ? => no equivalent, use Nullable<>

Beware the required public keyword for an enum, a necessary evil since C++11 adopted the enum class syntax.

Only the void, bool, short, int, float and double keywords match.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you. Can you provide a link to that information? – Christian St. Nov 05 '15 at 12:01
  • 1
    No, this is original work and not copied from anything. You'd have to read the entire language specification to find something back, Emca-372. We create references around here, little point in just reposting existing content. – Hans Passant Nov 05 '15 at 12:05
7

In C# long is a 64 bit data type. In C++ All we know about long is that it has to hold as much or more than an int and it is at least 32 bits. If you use a long long in c++ that is guaranteed to be at least 64 bits which will match what you have in C#.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Thank you for the explanation. What is meant, if we write other combinations, e.g., `long int x`? – Christian St. Nov 05 '15 at 11:08
  • @ChristianSt. In C++ there is no difference between `long` and `long int`. `long` is just the shorthand way to write it. http://stackoverflow.com/questions/18971732/difference-between-long-long-long-long-int-and-long-long-int-in-c – NathanOliver Nov 05 '15 at 12:52