Christian St.
Christian St.

Reputation: 1911

What is the difference between type "long" in C++/CLI and C#?

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#)?

Upvotes: 2

Views: 3207

Answers (2)

Hans Passant
Hans Passant

Reputation: 941635

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.

Upvotes: 10

NathanOliver
NathanOliver

Reputation: 180660

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#.

Upvotes: 7

Related Questions