Question

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 ?

Was it helpful?

Solution

The only difference is probably being explicit about the range of values supported by int and System::Int32.

System::Int32 makes the 32-bitness more explicit to those reading the code. One should use int where there is just need of 'an integer', and use System::Int32 where the size is important (cryptographic code, structures) so readers will be clear of it while maintaining the same.

Using int or System::Int32the resulting code will be identical: the difference is purely one of explicit readability or code appearance.

OTHER TIPS

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.

No. One of them is just an alias for the other.

int in C# is the same as System.Int32 of .Net.

Apparently, unlike C and regular C++, C# and C++/CLI have an int type that is always 32 bits long.

This is confirmed by MSDN, so the answer is that there is no difference between the two, other than the fact that int it considerably faster to type than System::int32.

The two, int and int32, are indeed synonymous; int will be a little more familiar looking, Int32 makes the 32-bitness more explicit to those reading your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top