4

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 ?

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147

6 Answers6

4

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.

dan04
  • 87,747
  • 23
  • 163
  • 198
2

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @ViTo Brothers Apoyan: Have you read the modified answer? I don't think theres anything wrong. – Alok Save May 23 '11 at 05:53
  • @ViTo Brothers Apoyan: No difference at all. – Alok Save May 23 '11 at 06:16
  • I would consider `int` more portable and scalable than `System::Int32`. With later compiler versions and advances in hardware, the size of `int` could change for newer releases (requires recompiling) of the same code. If you specify `System::Int32`, it hard codes the size and range of that variable. – Charity Leschinski Dec 03 '12 at 15:51
1

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

jmoreno
  • 12,752
  • 4
  • 60
  • 91
1

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

Alex R.
  • 4,664
  • 4
  • 30
  • 40
1

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.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • If int is faster then System::Int32 I must always use int. And for what I need System::Int32 ? – Viktor Apoyan May 23 '11 at 05:48
  • 1
    You misunderstood me. `int` is shorter than `System::int32`, which means it takes less time to type. :) Other than that, they are just different names for the same thing. – dandan78 May 23 '11 at 05:54
0

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.

Jainendra
  • 24,713
  • 30
  • 122
  • 169