📜  C#中的int关键字

📅  最后修改于: 2021-05-30 00:47:12             🧑  作者: Mango

关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 int是用于声明变量的关键字,该变量可以存储值的整数类型(带符号整数),范围从-2,147,483,6482,147,483,647 。它是System.Int32的别名。

句法:

int variable_name = value;

Int关键字在内存中占用4个字节(32位)的空间。

例子:

Input: num: -245

Output: num: -245
        Size of an int variable: 4

Input: num = 7923645

Output: Type of num: System.Int32
        num: 7923645
        Size of a int variable: 4

范例1:

// C# program for int keyword
using System;
using System.Text;
  
class geeks {
  
    static void Main(string[] args)
    {
        // variable declaration
        int num = -245;
  
        // to print value
        Console.WriteLine("num: " + num);
  
        // to print size
        Console.WriteLine("Size of a int variable: " + sizeof(int));
    }
}

输出:

num: -245
Size of a int variable: 4

范例2:

// C# program for int keyword
using System;
using System.Text;
  
namespace Test {
  
class geeks {
  
    static void Main(string[] args)
    {
        // variable declaration
        int num = 7923645;
  
        // to print type of variable
        Console.WriteLine("Type of num: " + num.GetType());
  
        // to print value
        Console.WriteLine("num: " + num);
  
        // to print size
        Console.WriteLine("Size of a int variable: " + sizeof(int));
  
        // to print minimum & maximum value of int
        Console.WriteLine("Min value of int: " + int.MinValue);
        Console.WriteLine("Max value of int: " + int.MaxValue);
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

输出:

Type of num: System.Int32
num: 7923645
Size of a int variable: 4
Min value of int: -2147483648
Max value of int: 2147483647

范例3:

// C# program for int keyword
using System;
using System.Text;
  
class geeks {
  
    static void Main(string[] args)
    {
  
        // variable declaration
        int num1 = 2147483650;
  
        // to print value
        Console.WriteLine("num1: " + num1);
  
        // variable declaration
        int num = 792.53;
  
        // to print value
        Console.WriteLine("num: " + num);
    }
}

错误:输入错误的整数并且输入数字超出范围时