Click to See Complete Forum and Search --> : question about intrinsic types
zildjohn01
Apr 20th, 2005, 08:22 PM
hi, i am relatively new to C# and have a question. Is there any difference between a System.Int32 and int?
StrangerInBeijing
Apr 20th, 2005, 08:36 PM
You can write this:int number = 42;
Or you can write this:
System.Int32 number = 42;
As far as the compiler is concerned, these lines of code are eactly the same. The int keyword and System.Int32 are completely interchangeable. And if a using System; directive is in scope, the int keyword and Int32 are also
completely interchangeable.
This keyword-type equivalence shouldn't come as too much of a surprise when
you remember that the object keyword is an alias for the System.Object class
Lemme know if you need a good C# book. Got this great book, and it also came in .chm format on cd! I like it.
nemaroller
Apr 20th, 2005, 10:36 PM
The whole thing boils down to this...
An Int32 is a hard-defined 32 bit integer. The int keyword is currently implemented to alias the Int32 type, but it doesn't necessarily have to be. The .Net framework could conceivably be changed on a 64bit platform to have int alias an Int64 type, which would be the optimal integer type on a 64bit platform.
Int32's are useful when you need to make sure that an integer is 4 bytes long (Windows API calls for example). So if you make calls to unmanaged code that expects a 4-byte integer... you would most certainly want to declare it a parameter you pass to it as Int32.
Now, the truth of the matter is... the int intrinsic type may never alias an integer larger than 4 bytes - but you never really know.
Bottomline: If you want people who see your code to know that the int you declared must be 4 bytes... use an Int32. If you don't care how many bytes it occupies now or in the future... use int. I imagine int will be your choice 99.9999995% of the time.
brad jones
Apr 20th, 2005, 11:30 PM
As an "FYI", the following table is from the book I wrote, Teach Yourself the C# Language in 21 Days. You'll see the C# data type and the .NET data type they translate to.
Table 2.4 -- C# and .NET Data Types
C# Data Type = .NET Data Type
sbyte = System.SByte
byte = System.Byte
short = System.Int16
ushort = System.UInt16
int = System.Int32
uint = System.UInt32
long = System.Int64
ulong = System.UInt64
char = System.Char
float = System.Single
double = System.Double
bool = System.Boolean
decimal = System.Decimal
StrangerInBeijing
Apr 20th, 2005, 11:54 PM
So the answer to John's question is a plain "No" (according to Brad's table), but in the soon future we will use Int32 and Int64 to distinguish between the two for in case the user got a 64bit system (Nema)
right? that means no, difference, but better coding practice to use Int32? Just making my own conclusions.
nemaroller
Apr 21st, 2005, 07:00 AM
I would say that 99.95% of the time, you should use int. On a 64bit platform, or 128bit platforms in the future - the int data type could ideally be compiled to the optimal integer length of the platform it is targeted to run on.
Only when dealing with calls that expect a 4bit integer, use System.Int32.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.