开发者

alias for int32,int64

class Program
{
   static void Main(string[] args)
   {
       Int64 a = Int64.MaxValue;
       Int64 b= Int64.MinValue;

       try
       {
          checked
          {
             Int64 m = a * b;

          }
       }
       catch (OverflowException ex)
       {
          Console.WriteLine("over flow exception");
          Console.Read();
       }
   }
}

if the variables are declared开发者_JAVA百科 as int, i am getting the compilation error, conversion is requreid from int to long.

  1. why am I getting this error although i am using int.
  2. What are the alias Int32 and Int64
  3. When to use Int32 and Int64, does it depend on OS?


Int32 corresponds to int, Int64 corresponds to long. For most general uses you can use 32-bit integers; however if you need very large numbers use long integers (64-bit).

When you assign Int64.MaxValue to an int, you're implicitly converting a long (Int64) to an int (Int32), which doesn't work. Besides, that value is way too large to fit in a 32-bit integer.


Int32 = int
Int64 = long

CTS implementation guarantee the types to be portable on any CPU/OS.


int is int32 and long is int64.

http://msdn.microsoft.com/en-us/library/ctetwysk(v=VS.100).aspx


What are the alias Int32 and Int64

Alias of Int32 is int. Alias of Int64 is long.

When to use Int32 and Int64, does it depend on OS?

Whether to use Int32 or Int64 depends on the application (range, sign requirement). And No, it does NOT depend on the OS. int will always be 4 byte long Int32 as long as .NET framework is concerned. Please also note that alias is a feature of compiler. So long does make sense when you are programming in C# but consider VB.NET developers who don't necessary like the property names like Array.LongLength since long doesn't make sense to them.


1) You shouldn't get this error if you use int everywhere. The following works fine:

using System;

class Program
{
    static void Main(string[] args)
    {
        int a = int.MaxValue;
        int b = int.MinValue;

        try
        {
            checked
            {
                int m = a * b;

            }
        }
        catch (OverflowException ex)
        {
            Console.WriteLine("over flow exception");
            Console.Read();

        }
    }
}

2) Int32 is aliased using int, Int64 is aliased using long

3) This is not platform specific, but rather defined by the C# specification. It is the same on all platforms. Use Int32 (or int) when you need a 32 bit integer, and Int64 (or long) when you need a 64 bit integer. This is more of a logic/algorithmic decision, not a platform related one.


System.Int32 and System.Int64 are types defined by the CLI standard (ECMA-335). They're basically the CLR/framework versions of the types for a 32 and 64 bit integer, and are guaranteed to be portable.

Some languages, like C#, define aliases to those types -- C# defines int to be the same as System.Int32 and long to be the same as System.Int64 -- consequently those types are also portable.

As to which you use (CLR version versus language version, at least), it is by and large a matter of style, especially if you are using only one language. I personally prefer to use the language-specified aliases for CLR types when possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜