在C#中,字节结构用于表示8位无符号整数。 Byte是一个不可变的值类型,Byte的范围是0到255。此类允许您创建Byte数据类型,并且可以对其进行数学和按位运算,例如加法,减法,乘法,除法,XOR和AND等。
领域
Field | Description |
---|---|
MaxValue | It is the largest possible value of a Byte. This field is constant. |
MinValue | It is the smallest possible value of a Byte. This field is constant. |
例子:
// C# program to illustrate the concept
// of MaxValue and MinValue fields in Byte
using System;
public class GFG {
// Main method
static public void Main()
{
// Display the minimum and
// the maximum value of Byte
Console.WriteLine("The minimum value "+
"of Byte: {0}", Byte.MinValue);
Console.WriteLine("The maximum value "+
"of Byte: {0}", Byte.MaxValue);
}
}
输出:
The minimum value of Byte: 0
The maximum value of Byte: 255
方法
Method | Description |
---|---|
CompareTo(Object) | Compares the current instance to a specified object and returns a sign of their relative values. |
CompareTo(Byte) | Compares this instance to a specified 8-bit unsigned integer and returns an indication of their relative values. |
Equals(Object) | To get a value which indicates whether the current instance is equal to a specified object or not. |
Equals(Byte) | Returns a value indicating whether this instance and a specified Byte object represent the same value. |
GetHashCode() | Returns the hash code for this instance. |
GetTypeCode() | Returns the TypeCode for value type Byte. |
Parse() | Converts the string representation of a number to its Byte equivalent. |
ToString() | Converts the value of the current Byte object to its equivalent string representation. |
TryParse() | Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded. |
例子:
// C# program to illustrate the concept
// of CompareTo(Byte) method in Byte
using System;
public class GFG {
// Main method
static public void Main()
{
// val1, val2, and val3 are of byte type
byte val1 = 32;
byte val2 = 40;
byte val3 = 10;
// Display the comparison
// Using CompareTo(Byte) method
Console.WriteLine("Comparison 1: {0}",
val1.CompareTo(val2));
Console.WriteLine("Comparison 2: {0}",
val2.CompareTo(val3));
Console.WriteLine("Comparison 3: {0}",
val3.CompareTo(val3));
Console.WriteLine("Comparison 4: {0}",
val1.CompareTo(val3));
}
}
输出:
Comparison 1: -8
Comparison 2: 30
Comparison 3: 0
Comparison 4: 22
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.byte?view=netframework-4.7.2