Locked What is the size of int in c#

  • Monday, August 29, 2011 12:44 PM
     
     

    Hi

    The book I am reading says in C# int is 32-bit.  Is it 32-bit on all CPUs (32 bit cpu, 64 bii cpu)?.  How is cpu related to size of variables?.  Why people say compiling to a specific CPU improves performance?.

     

    Thanks

    Ravi 

All Replies

  • Monday, August 29, 2011 12:55 PM
     
     Answered

    int is always 32bit in C#.

    internally, the references are using 32bit or 64bit, depending, what kind of  process/CPU you have. That means, that structures/classes can be different in size, depending on the process on which your C#-program is running. BUT an int variable is always 32 bit.

     

  • Monday, August 29, 2011 1:39 PM
     
     

     Why people say compiling to a specific CPU improves performance?.

    Where did you saw this ? AFAIK it was true when we changed from 16 bit to 32 bit mode as we had a new instruction set.

    This is AFAIK not true for 64 bit. My understanding is that primary benefit is to be able to address much more memory than previously possible (which coukld marginaly improve performance but shouldn't be considered as a goal, the goal is being able to write an app able to easily address much more memory).

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
  • Monday, August 29, 2011 1:59 PM
     
     Answered

    Regarding size:

    The reference types (object references and pointers) are the size of a memory address, which would be 32 bits (4 bytes) on a 32-bit platform, and 64-bits (8 bytes) on a 64-bit platform. All the other data types are defined by the language, not the CPU or OS.  See the reference chart below.

     

    Short Name

    .NET Class

    Type

    Width

    Range (bits)

    byte

    Byte

    Unsigned integer

    8

    0 to 255

    sbyte

    SByte

    Signed integer

    8

    -128 to 127

    int

    Int32

    Signed integer

    32

    -2,147,483,648 to 2,147,483,647

    uint

    UInt32

    Unsigned integer

    32

    0 to 4294967295

    short

    Int16

    Signed integer

    16

    -32,768 to 32,767

    ushort

    UInt16

    Unsigned integer

    16

    0 to 65535

    long

    Int64

    Signed integer

    64

    -9223372036854775808 to 9223372036854775807

    ulong

    UInt64

    Unsigned integer

    64

    0 to 18446744073709551615

    float

    Single

    Single-precision floating point type

    32

    -3.402823e38 to 3.402823e38

    double

    Double

    Double-precision floating point type

    64

    -1.79769313486232e308 to 1.79769313486232e308

    char

    Char

    A single Unicode character

    16

    Unicode symbols used in text

    bool

    Boolean

    Logical Boolean type

    8

    True or false

    object

    Object

    Base type of all other types

     

     

    string

    String

    A sequence of characters

     

     

    decimal

    Decimal

    Precise fractional or integral type that can represent decimal numbers with 29 significant digits

    128

    ±1.0 × 10e−28 to ±7.9 × 10e28

    See http://msdn.microsoft.com/en-us/library/ms228360.aspx

  • Monday, August 29, 2011 3:16 PM
     
     Answered

    Hi Ravi,

    Is C# int 32-bit on all CPUs?
    Yes.

    Why people say compiling to a specific CPU improves performance?

    In C#, there is no such thing as compiling to a specific CPU. The code is always compiled to IL (intermediate language). Therefore, the EXE file contains mostly IL code, and only a tiny section of native code for the initialization of CLR. When the EXE file is loaded in memory, IL code is compiled to platform-specific native code on-the-fly. This is called just-in-time compilation (JIT).

    JIT compilation is performed every time a new IL module is loaded in memory. The time delay required for JIT is generally unnoticeable. However if you want to get rid of this delay, you can pre-generate native code translations of your managed assemblies using NGEN. NGEN is a command-line utility that generates and caches the native image of your managed assemblies.

    The native image generated by NGEN is specific to your CPU.

    P.S.: The compiler has a "target platform" switch, which can be set to x86, Itanium, x64, or anycpu. Don't be misled by this switch. You can use this switch to prevent your application from running on platforms you don't support. You may need this switch if you have unmanaged DLL dependencies. This switch does not generate native code for a specific CPU.

    I hope this information helps you...

    Best regards,

    Elyo Ravuna

    • Proposed As Answer by eravuna Monday, August 29, 2011 3:16 PM
    • Marked As Answer by RaviAustin Saturday, October 20, 2012 4:31 PM
    •  
  • Monday, August 29, 2011 9:06 PM
     
     
    Thanks everyone for the response. All of the responses were helpful to me.
    Ravi
  • Monday, August 29, 2011 9:49 PM
     
     

    Hi Reed,

    I have a follow-up question on this.  There is a compile option in Visual Studio to select "AnyCPU".  Is there a reason, c# program is compiled to a specific CPU?.Other options include "x86" and "64bit"  


    Ravi
  • Monday, August 29, 2011 10:10 PM
     
     Answered

    For example if your assembly depends on a 64bit DLL file, then you have to select 64bit. This way, your application will refuse to run on x86 platforms.

    But, you will get exactly the same IL code if you select 64bit or x86. Selecting 64bit or x86 is not about generating an EXE optimized for a specific platform, it is only about checking the environment at the beginning of the execution.

    Unless you have unmanaged dependencies that are known to cause problems on some platforms, best-practice is leaving this option anycpu.

    Best regards,

    Elyo Ravuna


    • Proposed As Answer by eravuna Monday, August 29, 2011 10:20 PM
    • Edited by eravuna Tuesday, August 30, 2011 12:15 PM
    • Marked As Answer by RaviAustin Saturday, October 20, 2012 4:32 PM
    •  
  • Tuesday, August 30, 2011 12:10 PM
     
     
    If your assembly depends on a 64bit DLL file, then you have to select 64bit. This way, your application will refuse to run on x86 platforms.

    And if your application depends on a 32bit DLL file, you have to select x86. That way your application will run in 32bit mode on 64bit platforms.
  • Wednesday, August 31, 2011 3:21 AM
     
     
    The dependent DLL files that we are talking here,  are these .net run time dlls?.  I googled for .net runtime versions for 32bit and 64 bit.  Infact they have separate .net runtimes for 32 and 64 bits.   
    Ravi
  • Wednesday, August 31, 2011 12:44 PM
     
     

    No.. usually these dependend DLL are COM-DLLs. Often programmed in C++.

    These C++ DLL are platform/processor dependend, which means: you have to start your process/program with the platform which these DLLs have.

  • Wednesday, August 31, 2011 2:10 PM
     
     

    Could be any dll compiled specifically for 32bit or 64 bit. If your application is compiled for AnyCPU or 64bit and you try loading a 32bit dll, you're in trouble.

    You can also have your code dynamically load a dll depending on which platform you're working. I once made an ApiFactory class able to select which dll to use for p/invoking.