Reputation: 10755
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 ?
Upvotes: 4
Views: 6195
Reputation: 25153
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.
Upvotes: 0
Reputation: 91217
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 toSystem::SByte
.unsigned char
maps toSystem::Byte
.- If a plain
char
is signed,char
maps toSystem::SByte
; otherwise, it maps toSystem::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 int
s.
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
.
Upvotes: 4
Reputation: 206636
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::Int32
the resulting code will be identical: the difference is purely one of explicit readability or code appearance.
Upvotes: 2
Reputation: 13925
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
.
Upvotes: 1