关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 int是用于声明变量的关键字,该变量可以存储值的整数类型(带符号整数),范围从-2,147,483,648到2,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);
}
}
错误:输入错误的整数并且输入数字超出范围时
Constant value `2147483650′ cannot be converted to a `int’
Cannot implicitly convert type `double’ to `int’