// C# 中的 int 类型示例
using System;
class Program
{
static void Main()
{
// 声明一个 int 变量
int number = 10;
// 输出 int 变量的值
Console.WriteLine("The value of the integer is: " + number);
// int 类型的取值范围
Console.WriteLine("The minimum value of an int is: " + int.MinValue);
Console.WriteLine("The maximum value of an int is: " + int.MaxValue);
// 将字符串转换为 int
string strNumber = "20";
int parsedNumber;
bool isParsed = int.TryParse(strNumber, out parsedNumber);
if (isParsed)
{
Console.WriteLine("Successfully parsed string to int: " + parsedNumber);
}
else
{
Console.WriteLine("Failed to parse string to int.");
}
// int 类型的算术运算
int a = 5;
int b = 3;
Console.WriteLine("Addition: " + (a + b));
Console.WriteLine("Subtraction: " + (a - b));
Console.WriteLine("Multiplication: " + (a * b));
Console.WriteLine("Division: " + (a / b));
Console.WriteLine("Modulus: " + (a % b));
}
}
int number = 10;
表示声明一个名为 number
的整数变量,并将其初始化为 10。Console.WriteLine
输出变量的值。int.MinValue
和 int.MaxValue
分别表示 int
类型的最小值和最大值。int.TryParse
方法用于将字符串安全地转换为整数,如果转换成功则返回 true
,否则返回 false
。上一篇:c#编程软件
下一篇:c# readonly
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站