using System;
namespace UShortExample
{
class Program
{
static void Main(string[] args)
{
// ushort 是一个无符号的 16 位整数类型,取值范围是 0 到 65535。
ushort myUShort = 12345;
Console.WriteLine("The value of myUShort is: " + myUShort);
// 尝试将一个超出范围的值赋给 ushort 变量会引发编译错误
// ushort tooBig = 70000; // 这行代码会导致编译错误
// 演示如何将 int 类型转换为 ushort 类型
int intValue = 30000;
if (intValue >= ushort.MinValue && intValue <= ushort.MaxValue)
{
ushort convertedUShort = (ushort)intValue;
Console.WriteLine("Converted int to ushort: " + convertedUShort);
}
else
{
Console.WriteLine("intValue is out of the range of ushort.");
}
}
}
}
ushort
是 C# 中的一种无符号 16 位整数类型,取值范围是 0 到 65535。ushort
类型的变量 myUShort
并赋予它一个值 12345
。ushort
范围的值赋给 ushort
变量,会导致编译错误。int
类型的值安全地转换为 ushort
类型,并进行了范围检查以确保不会发生溢出。上一篇:c# datatable 删除行
下一篇:c# byte[]转string
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站