Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# ushort

作者:战天独尊   发布日期:2025-05-05   浏览:57

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

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c#高并发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站