在C#中,UInt16结构用于表示从0到65535范围的16位无符号整数(也称为ushort数据类型)。它提供了不同类型的方法来执行各种操作,例如比较此类型的实例,将实例的值转换为其字符串表示形式,将数字的字符串表示形式转换为该类型的实例等。系统名称空间。 UInt16结构继承了ValueType类,后者继承了Object类。
领域
Field | Description |
---|---|
MaxValue | Represents the largest possible value of UInt16. This field is constant. |
MinValue | Represents the smallest possible value of UInt16. This field is constant. |
例子:
// C# program to illustrate the
// fields of UInt16 struct
using System;
class GFG {
// Main Method
static public void Main()
{
// Unsigned 16-bit integer
ushort val = 295;
// Checking the unsigned integer
if (val.Equals(UInt16.MinValue))
{
Console.WriteLine("Equal to MinValue..!");
}
else if (val.Equals(UInt16.MaxValue))
{
Console.WriteLine("Equal to MaxValue");
}
else
{
Console.WriteLine("Not equal");
}
}
}
输出:
Not equal
方法
Method | Description |
---|---|
CompareTo() | Compares the current instance to a specified object or UInt16 and returns an indication of their relative values. |
Equals() | Returns a value which shows whether the current instance is equal to a specified object or UInt16. |
GetHashCode() | Returns the hash code for this instance. |
GetTypeCode() | Returns the TypeCode for value type UInt16. |
Parse() | Converts the string representation of a number to its 16-bit unsigned integer equivalent. |
ToString() | Converts the numeric value of this instance to its equivalent string representation. |
TryParse() | Converts the string representation of a number to its 16-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed. |
例子:
// C# program to illustrate how to get the hash
// code of the 16-bit Unsigned integer
using System;
class GFG {
// Main Method
static public void Main()
{
// UInt16 variable
ulong myval = 545;
// Get the hash code
// Using GetHashCode Method
int res = myval.GetHashCode();
Console.WriteLine("The hash code of myval is: {0}", res);
}
}
输出:
The hash code of myval is: 545
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.uint16?view=netframework-4.8