Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between int and System::Int32

As I'm writing applications using C++ .NET framework I want to ask a question.

When I am writing code exactly for "for loop" I always use this style of coding:

for( int i=0; i<somevalue; ++i ) {
    // Some code goes here.
}

but as I am using C++ .NET I can write

for( System::Int32 i=0; i<somevalue; ++i ) {
    // Some code goes here.
}

I think there are no difference Am I right ? If no can you explain whats the difference between this two ways and. witch one is more effective ?

like image 310
Viktor Apoyan Avatar asked Oct 17 '25 18:10

Viktor Apoyan


1 Answers

The C++/CLI language specification (p. 50) says that:

The fundamental types map to corresponding value class types provided by the implementation, as follows:

  • signed char maps to System::SByte.
  • unsigned char maps to System::Byte.
  • If a plain char is signed, char maps to System::SByte; otherwise, it maps to System::Byte.
  • For all other fundamental types, the mapping is implementation-defined [emphasis mine].

This differs from C#, in which int is specifically defined as an alias for System.Int32. In C++/CLI, as in ISO C++, an int has "the natural size suggested by the architecture of the execution environment", and is only guaranteed to have at least 16 bits.

In practice, however, .NET does not run on 16-bit systems, so you can safely assume 32-bit ints.

But be careful with long: Visual C++ defines it as 32-bit, which is not the same as a C# long. The C++ equivalent of System::Int64 is long long.

like image 59
dan04 Avatar answered Oct 19 '25 12:10

dan04