📜  C#中Int16,Int32和Int64之间的区别

📅  最后修改于: 2021-05-29 19:12:01             🧑  作者: Mango

Int16 结构用于表示16位带符号整数。所述的Int16可存储两种类型的值,包括范围之间负的和正的-32768到32767。

例子 :

C#
// C# program to show the
// Int16 Struct usage
  
using System;
using System.Text;
  
public
class GFG {
  
  // Main Method
  static void Main(string[] args) {
  
    // printing minimum & maximum values
    Console.WriteLine("Minimum value of Int16: " 
                      + Int16.MinValue);
    Console.WriteLine("Maximum value of Int16: " 
                      + Int16.MaxValue);
    Console.WriteLine();
  
    // Int16 array
    Int16[] arr1 = {-3, 0, 1, 3, 7};
  
    foreach (Int16 i in arr1)
    { 
      Console.WriteLine(i);
    }
  }
}


C#
// C# program to show the
// Int32 struct usage
  
using System;
using System.Text;
  
public
class GFG {
  
  // Main Method
  static void Main(string[] args) {
  
    // printing minimum & maximum values
    Console.WriteLine("Minimum value of Int32: " 
                      + Int32.MinValue);
    Console.WriteLine("Maximum value of Int32: " 
                      + Int32.MaxValue);
    Console.WriteLine();
  
    // Int32 array
    Int32[] arr1 = {-3, 0, 1, 3, 7};
  
    foreach (Int32 i in arr1)
    { 
      Console.WriteLine(i);
    }
  }
}


C#
// C# program to show 
// Int64 struct usage
using System;
using System.Text;
  
public class GFG {
  
  // Main Method
  static void Main(string[] args) {
  
    // printing minimum & maximum values
    Console.WriteLine("Minimum value of Int64: " 
                      + Int64.MinValue);
    Console.WriteLine("Maximum value of Int64: " 
                      + Int64.MaxValue);
    Console.WriteLine();
  
    // Int64 array
    Int64[] arr1 = {-3, 0, 1, 3, 7};
  
    foreach (Int64 i in arr1)
    { 
      Console.WriteLine(i);
    }
  }
}


输出:

Minimum value of Int16: -32768
Maximum value of Int16: 32767

-3
0
1
3
7

Int32 结构用于表示32位带符号整数。所述的Int32可存储两种类型的值,包括范围之间负的和正的-2147483648到2147483647。

例子 :

C#

// C# program to show the
// Int32 struct usage
  
using System;
using System.Text;
  
public
class GFG {
  
  // Main Method
  static void Main(string[] args) {
  
    // printing minimum & maximum values
    Console.WriteLine("Minimum value of Int32: " 
                      + Int32.MinValue);
    Console.WriteLine("Maximum value of Int32: " 
                      + Int32.MaxValue);
    Console.WriteLine();
  
    // Int32 array
    Int32[] arr1 = {-3, 0, 1, 3, 7};
  
    foreach (Int32 i in arr1)
    { 
      Console.WriteLine(i);
    }
  }
}

输出:

Minimum value of Int32: -2147483648
Maximum value of Int32: 2147483647

-3
0
1
3
7

Int64 结构用于表示64位带符号整数。所述的Int64可以两种类型的值,包括-9,223,372,036,854,775,808范围之间负的和正的存储9,223,372,036,854,775,807

例子 :

C#

// C# program to show 
// Int64 struct usage
using System;
using System.Text;
  
public class GFG {
  
  // Main Method
  static void Main(string[] args) {
  
    // printing minimum & maximum values
    Console.WriteLine("Minimum value of Int64: " 
                      + Int64.MinValue);
    Console.WriteLine("Maximum value of Int64: " 
                      + Int64.MaxValue);
    Console.WriteLine();
  
    // Int64 array
    Int64[] arr1 = {-3, 0, 1, 3, 7};
  
    foreach (Int64 i in arr1)
    { 
      Console.WriteLine(i);
    }
  }
}

输出:

Minimum value of Int64: -9223372036854775808
Maximum value of Int64: 9223372036854775807

-3
0
1
3
7

C#中Int16,Int32和Int64之间的区别

Sr.No

INT16

INT32

INT64

1.

Int16 is used to represents 16-bit signed integers.  Int32 is used to represents 32-bit signed integers . Int64 is used to represents 64-bit signed integers.

2.

Int16 stands for signed integer. Int32 also stands for signed integer. Int64 also stands for signed integer.

3.

It can store negative and positive integers. It can also store negative and positive integers. It can also store negative and positive integers.

4.

It takes 2-bytes  space in the memory. It takes 4-bytes  space in the memory. It takes 8-bytes  space in the memory.

5.

The range of Int16 is from -32768 to +32767. The range of Int32 is from -2147483648 to +2147483647. The range of Int64 is from -9223372036854775808 to +9223372036854775807.

 6.

 Syntax to declare the Int16 :

Int16 variable_name;

 Syntax to declare the Int32 :

Int32 variable_name;

 Syntax to declare the Int64 :

Int64 variable_name;