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

ぃ、小莉子 提交于 2021-02-07 04:32:05

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/33523478/what-is-the-difference-between-type-long-in-c-cli-and-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!