Results 1 to 6 of 6

Thread: question about intrinsic types

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Question question about intrinsic types

    hi, i am relatively new to C# and have a question. Is there any difference between a System.Int32 and int?

  2. #2
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: question about intrinsic types

    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.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: question about intrinsic types

    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.

  4. #4
    ex-Administrator brad jones's Avatar
    Join Date
    Nov 2002
    Location
    Indianapolis
    Posts
    6,614

    Re: question about intrinsic types

    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
    Have you given out your reputation points today? Select the Rate This Post link to give points for good posts!
    -------------------------------------------------------------
    Brad! Jones
    Lots of Software, LLC
    (I wrote: C Programming in One Hour a Day) (Dad Jokes Book) (Follow me on Twitter)

    --------------------------------------------------------------

  5. #5
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: question about intrinsic types

    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.

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: question about intrinsic types

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width