Byte Byte Byte Byte Struct

定义

表示一个 8 位无符号整数。Represents an 8-bit unsigned integer.

public value class Byte : IComparable, IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, IFormattable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, IFormattable
type byte = struct
    interface IFormattable
    interface IConvertible
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IConvertible, IEquatable(Of Byte), IFormattable
继承
属性
实现

注解

Byte 是不可变的值类型,表示无符号的整数的值范围从 0 (表示由Byte.MinValue常量) 到 255 之间 (表示通过Byte.MaxValue常量)。Byte is an immutable value type that represents unsigned integers with values that range from 0 (which is represented by the Byte.MinValue constant) to 255 (which is represented by the Byte.MaxValue constant). .NET Framework 还包括 8 位带符号的整数值类型, SByte,它表示介于-128 到 127 之间的值。The .NET Framework also includes a signed 8-bit integer value type, SByte, which represents values that range from -128 to 127.

实例化的字节值Instantiating a Byte Value

可以实例化Byte几种方式的值:You can instantiate a Byte value in several ways:

  • 您可以声明Byte变量并将其分配的范围内的整数值Byte数据类型。You can declare a Byte variable and assign it a literal integer value that is within the range of the Byte data type. 下面的示例声明了两个Byte变量并将其分配这种方式中的值。The following example declares two Byte variables and assigns them values in this way.

    byte value1 = 64;
    byte value2 = 255;
    
    Dim value1 As Byte = 64
    Dim value2 As Byte = 255
    
  • 可以将非字节数字值分配给一个字节。You can assign a non-byte numeric value to a byte. 这是收缩转换,因此它需要在 C# 中的强制转换运算符,并在 Visual Basic 中的转换方法如果Option Strict上。This is a narrowing conversion, so it requires a cast operator in C# and a conversion method in Visual Basic if Option Strict is on. 如果非字节值为SingleDouble,或Decimal包括小数部分,其小数部分处理的值取决于编译器执行转换。If the non-byte value is a Single, Double, or Decimal value that includes a fractional component, the handling of its fractional part depends on the compiler performing the conversion. 以下示例将分配到多个数值Byte变量。The following example assigns several numeric values to Byte variables.

    int int1 = 128;
    try {
       byte value1 = (byte) int1;
       Console.WriteLine(value1);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of a byte.", int1);
    }
    
    double dbl2 = 3.997;
    try {
       byte value2 = (byte) dbl2;
       Console.WriteLine(value2);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of a byte.", dbl2);
    }
    // The example displays the following output:
    //       128
    //       3
    
    Dim int1 As Integer = 128
    Try
       Dim value1 As Byte = CByte(int1)
       Console.WriteLine(value1)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of a byte.", int1)
    End Try
    
    Dim dbl2 As Double = 3.997
    Try
       Dim value2 As Byte = CByte(dbl2)
       Console.WriteLine(value2)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of a byte.", dbl2)
    End Try   
    ' The example displays the following output:
    '       128
    '       4
    
  • 您可以调用的方法Convert类来转换任何受支持的类型为Byte值。You can call a method of the Convert class to convert any supported type to a Byte value. 这可能是因为Byte支持IConvertible接口。This is possible because Byte supports the IConvertible interface. 下面的示例演示转换的数组Int32值到Byte值。The following example illustrates the conversion of an array of Int32 values to Byte values.

    int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue };
    byte result;
    foreach (int number in numbers)
    {
       try {
          result = Convert.ToByte(number);
          Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", 
                            number.GetType().Name, number, 
                            result.GetType().Name, result);
       }                     
       catch (OverflowException) {
          Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", 
                            number.GetType().Name, number);
       }
    }
    // The example displays the following output:
    //       The Int32 value -2147483648 is outside the range of the Byte type.
    //       The Int32 value -1 is outside the range of the Byte type.
    //       Converted the Int32 value 0 to the Byte value 0.
    //       Converted the Int32 value 121 to the Byte value 121.
    //       The Int32 value 340 is outside the range of the Byte type.
    //       The Int32 value 2147483647 is outside the range of the Byte type.      
    
    Dim numbers() As Integer = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }
    Dim result As Byte
    For Each number As Integer In numbers
       Try
          result = Convert.ToByte(number)
          Console.WriteLIne("Converted the {0} value {1} to the {2} value {3}.", _
                            number.GetType().Name, number, _
                            result.GetType().Name, result)
       Catch e As OverflowException
          Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", _
                            number.GetType().Name, number)
       End Try
    Next
    ' The example displays the following output:
    '       The Int32 value -2147483648 is outside the range of the Byte type.
    '       The Int32 value -1 is outside the range of the Byte type.
    '       Converted the Int32 value 0 to the Byte value 0.
    '       Converted the Int32 value 121 to the Byte value 121.
    '       The Int32 value 340 is outside the range of the Byte type.
    '       The Int32 value 2147483647 is outside the range of the Byte type.      
    
  • 您可以调用ParseTryParse方法将转换的字符串表示形式Byte值设为ByteYou can call the Parse or TryParse method to convert the string representation of a Byte value to a Byte. 该字符串可包含十进制或十六进制数字。The string can contain either decimal or hexadecimal digits. 下面的示例说明了通过使用十进制和十六进制字符串的分析操作。The following example illustrates the parse operation by using both a decimal and a hexadecimal string.

    string string1 = "244";
    try {
       byte byte1 = Byte.Parse(string1);
       Console.WriteLine(byte1);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a byte.", string1);
    }
    catch (FormatException) {
       Console.WriteLine("'{0}' is out of range of a byte.", string1);
    }
    
    string string2 = "F9";
    try {
       byte byte2 = Byte.Parse(string2, 
                               System.Globalization.NumberStyles.HexNumber);
       Console.WriteLine(byte2);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a byte.", string2);
    }
    catch (FormatException) {
       Console.WriteLine("'{0}' is out of range of a byte.", string2);
    }
    // The example displays the following output:
    //       244
    //       249
    
    Dim string1 As String = "244"
    Try
       Dim byte1 As Byte = Byte.Parse(string1)
       Console.WriteLine(byte1)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a byte.", string1)
    Catch e As FormatException
       Console.WriteLine("'{0}' is out of range of a byte.", string1)
    End Try
    
    Dim string2 As String = "F9"
    Try
       Dim byte2 As Byte = Byte.Parse(string2,
                                 System.Globalization.NumberStyles.HexNumber)
       Console.WriteLine(byte2)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a byte.", string2)
    Catch e As FormatException
       Console.WriteLine("'{0}' is out of range of a byte.", string2)
    End Try
    ' The example displays the following output:
    '       244
    '       249
    

对字节值执行操作Performing Operations on Byte Values

Byte类型支持标准的数学运算,例如加法、 减法、 除法、 乘法、 减法、 求反和一元求反。The Byte type supports standard mathematical operations such as addition, subtraction, division, multiplication, subtraction, negation, and unary negation. 与其他整数类型,类似Byte类型还支持按位ANDORXOR,左移位和右移位运算符。Like the other integral types, the Byte type also supports the bitwise AND, OR, XOR, left shift, and right shift operators.

可以使用标准数字运算符比较两个Byte值,也可以调用CompareToEquals方法。You can use the standard numeric operators to compare two Byte values, or you can call the CompareTo or Equals method.

您还可以调用的成员Math类来执行各种数值操作,包括获得某个数字的商和整数除法运算的余数计算确定两个最大或最小值的绝对值整数,获取数字的符号和舍入数字。You can also call the members of the Math class to perform a wide range of numeric operations, including getting the absolute value of a number, calculating the quotient and remainder from integral division, determining the maximum or minimum value of two integers, getting the sign of a number, and rounding a number.

以字符串形式表示一个字节Representing a Byte as a String

Byte类型提供对标准和自定义数字格式字符串的完全支持。The Byte type provides full support for standard and custom numeric format strings. (有关详细信息,请参阅格式设置类型标准数字格式字符串,并自定义数字格式字符串。)但是,大多数情况下,字节值表示为一位到三位数字的值不带任何其他格式,或为两位数字的十六进制值。(For more information, see Formatting Types, Standard Numeric Format Strings, and Custom Numeric Format Strings.) However, most commonly, byte values are represented as one-digit to three-digit values without any additional formatting, or as two-digit hexadecimal values.

若要设置格式Byte值作为不带前导零的整数字符串,可以调用无参数ToString()方法。To format a Byte value as an integral string with no leading zeros, you can call the parameterless ToString() method. 通过使用"D"格式说明符,还可以包含指定的数目的字符串表示形式中的前导零。By using the "D" format specifier, you can also include a specified number of leading zeros in the string representation. 通过使用"X"格式说明符,可以表示Byte值作为十六进制字符串。By using the "X" format specifier, you can represent a Byte value as a hexadecimal string. 下面的示例中的数组元素进行格式设置Byte以下三种方式中的值。The following example formats the elements in an array of Byte values in these three ways.

byte[] numbers = { 0, 16, 104, 213 };
foreach (byte number in numbers) {
   // Display value using default formatting.
   Console.Write("{0,-3}  -->   ", number.ToString());
   // Display value with 3 digits and leading zeros.
   Console.Write(number.ToString("D3") + "   ");
   // Display value with hexadecimal.
   Console.Write(number.ToString("X2") + "   ");
   // Display value with four hexadecimal digits.
   Console.WriteLine(number.ToString("X4"));
}   
// The example displays the following output:
//       0    -->   000   00   0000
//       16   -->   016   10   0010
//       104  -->   104   68   0068
//       213  -->   213   D5   00D5      
Dim numbers() As Byte = { 0, 16, 104, 213 }
For Each number As Byte In numbers
   ' Display value using default formatting.
   Console.Write("{0,-3}  -->   ", number.ToString())
   ' Display value with 3 digits and leading zeros.
   Console.Write(number.ToString("D3") + "   ")
   ' Display value with hexadecimal.
   Console.Write(number.ToString("X2") + "   ")
   ' Display value with four hexadecimal digits.
   Console.WriteLine(number.ToString("X4"))
Next   
' The example displays the following output:
'       0    -->   000   00   0000
'       16   -->   016   10   0010
'       104  -->   104   68   0068
'       213  -->   213   D5   00D5      

此外可以设置格式Byte通过调用十进制或十六进制字符串值作为二进制、 八进制ToString(Byte, Int32)方法,并提供方法的第二个参数的基。You can also format a Byte value as a binary, octal, decimal, or hexadecimal string by calling the ToString(Byte, Int32) method and supplying the base as the method's second parameter. 下面的示例调用此方法来显示二进制、 八进制和十六进制形式表示的字节值数组。The following example calls this method to display the binary, octal, and hexadecimal representations of an array of byte values.

byte[] numbers ={ 0, 16, 104, 213 };
Console.WriteLine("{0}   {1,8}   {2,5}   {3,5}",
                  "Value", "Binary", "Octal", "Hex");
foreach (byte number in numbers) {
   Console.WriteLine("{0,5}   {1,8}   {2,5}   {3,5}",
                     number, Convert.ToString(number, 2),
                     Convert.ToString(number, 8),
                     Convert.ToString(number, 16));
}      
// The example displays the following output:
//       Value     Binary   Octal     Hex
//           0          0       0       0
//          16      10000      20      10
//         104    1101000     150      68
//         213   11010101     325      d5      
Dim numbers() As Byte = { 0, 16, 104, 213 }
Console.WriteLine("{0}   {1,8}   {2,5}   {3,5}", _
                  "Value", "Binary", "Octal", "Hex")
For Each number As Byte In numbers
   Console.WriteLine("{0,5}   {1,8}   {2,5}   {3,5}", _
                     number, Convert.ToString(number, 2), _
                     Convert.ToString(number, 8), _
                     Convert.ToString(number, 16))
Next      
' The example displays the following output:
'       Value     Binary   Octal     Hex
'           0          0       0       0
'          16      10000      20      10
'         104    1101000     150      68
'         213   11010101     325      d5      

处理非十进制字节值Working with Non-Decimal Byte Values

除了处理作为十进制值的各个字节,您可能想要执行使用字节值的按位运算或处理的字节数组或字节值的二进制或十六进制表示形式。In addition to working with individual bytes as decimal values, you may want to perform bitwise operations with byte values, or work with byte arrays or with the binary or hexadecimal representations of byte values. 例如,重载的BitConverter.GetBytes方法可以将每个基元数据类型转换为字节数组,并BigInteger.ToByteArray方法将BigInteger到字节数组的值。For example, overloads of the BitConverter.GetBytes method can convert each of the primitive data types to a byte array, and the BigInteger.ToByteArray method converts a BigInteger value to a byte array.

Byte 值以表示 8 位按其度量值,而无需符号位。Byte values are represented in 8 bits by their magnitude only, without a sign bit. 这一点上执行按位运算时,需要注意Byte值或使用的单个位。This is important to keep in mind when you perform bitwise operations on Byte values or when you work with individual bits. 若要执行数字、 布尔值或对任何两个非十进制值的比较运算,这两个值都必须使用相同的表示形式。In order to perform a numeric, Boolean, or comparison operation on any two non-decimal values, both values must use the same representation.

对两个执行操作的时间Byte值,值将共享相同的表示形式,因此结果是准确。When an operation is performed on two Byte values, the values share the same representation, so the result is accurate. 这一点在以下示例中,了屏蔽的最低顺序位Byte值,以确保它为偶数。This is illustrated in the following example, which masks the lowest-order bit of a Byte value to ensure that it is even.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { Convert.ToString(12, 16), 
                          Convert.ToString(123, 16), 
                          Convert.ToString(245, 16) };
      
      byte mask = 0xFE;
      foreach (string value in values) {
         Byte byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("{0} And {1} = {2}", byteValue, mask,  
                           byteValue & mask);
      }
   }
}
// The example displays the following output:
//       12 And 254 = 12
//       123 And 254 = 122
//       245 And 254 = 244
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { Convert.ToString(12, 16), _
                                 Convert.ToString(123, 16), _
                                 Convert.ToString(245, 16) }
      
      Dim mask As Byte = &hFE
      For Each value As String In values
         Dim byteValue As Byte = Byte.Parse(value, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("{0} And {1} = {2}", byteValue, mask, _ 
                           byteValue And mask)
      Next         
   End Sub
End Module
' The example displays the following output:
'       12 And 254 = 12
'       123 And 254 = 122
'       245 And 254 = 244

但是,未签名和签名位时,按位运算复杂的这一事实,SByte值使用符号数值表示法进行正值和负值的补数表示形式.On the other hand, when you work with both unsigned and signed bits, bitwise operations are complicated by the fact that the SByte values use sign-and-magnitude representation for positive values, and two's complement representation for negative values. 为了执行有意义的按位运算,必须将值转换为等效的两种表示形式,并且必须保留符号位有关的信息。In order to perform a meaningful bitwise operation, the values must be converted to two equivalent representations, and information about the sign bit must be preserved. 下面的示例这样做是为了出 2 和 4 个 8 位有符号和无符号值的数组的位掩码。The following example does this to mask out bits 2 and 4 of an array of 8-bit signed and unsigned values.

using System;
using System.Collections.Generic;
using System.Globalization;

public struct ByteString
{
   public string Value;
   public int Sign;
}

public class Example
{
   public static void Main()
   {
      ByteString[] values = CreateArray(-15, 123, 245);
      
      byte mask = 0x14;        // Mask all bits but 2 and 4.
      
      foreach (ByteString strValue in values) {
         byte byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})", 
                           strValue.Sign * byteValue, 
                           Convert.ToString(byteValue, 2), 
                           mask, Convert.ToString(mask, 2), 
                           (strValue.Sign & Math.Sign(mask)) * (byteValue & mask), 
                           Convert.ToString(byteValue & mask, 2));
      }
   }
   
   private static ByteString[] CreateArray(params int[] values)
   {
      List<ByteString> byteStrings = new List<ByteString>();

      foreach (object value in values) {
         ByteString temp = new ByteString();
         int sign = Math.Sign((int) value);
         temp.Sign = sign;

         // Change two's complement to magnitude-only representation.
         temp.Value = Convert.ToString(((int) value) * sign, 16);

         byteStrings.Add(temp);
      }
      return byteStrings.ToArray();
   }
}
// The example displays the following output:
//       -15 (1111) And 20 (10100) = 4 (100)
//       123 (1111011) And 20 (10100) = 16 (10000)
//       245 (11110101) And 20 (10100) = 20 (10100)
Imports System.Collections.Generic
Imports System.Globalization

Public Structure ByteString
   Public Value As String
   Public Sign As Integer
End Structure

Module Example
   Public Sub Main()
      Dim values() As ByteString = CreateArray(-15, 123, 245)
      
      Dim mask As Byte = &h14        ' Mask all bits but 2 and 4.
      
      For Each strValue As ByteString In values
         Dim byteValue As Byte = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})", _ 
                           strValue.Sign * byteValue, _ 
                           Convert.ToString(byteValue, 2), _
                           mask, Convert.ToString(mask, 2), _
                           (strValue.Sign And Math.Sign(mask)) * (byteValue And mask), _
                           Convert.ToString(byteValue And mask, 2))
      Next         
   End Sub
   
   Private Function CreateArray(ParamArray values() As Object) As ByteString()
      Dim byteStrings As New List(Of ByteString)
      For Each value As Object In values
         Dim temp As New ByteString()
         Dim sign As Integer = Math.Sign(value)
         temp.Sign = sign
         ' Change two's complement to magnitude-only representation.
         value = value * sign

         temp.Value = Convert.ToString(value, 16)
         byteStrings.Add(temp)
      Next
      Return byteStrings.ToArray()
   End Function   
End Module
' The example displays the following output:
'       -15 (1111) And 20 (10100) = 4 (100)
'       123 (1111011) And 20 (10100) = 16 (10000)
'       245 (11110101) And 20 (10100) = 20 (10100)

字段

MaxValue MaxValue MaxValue MaxValue

表示 Byte 的最大可能值。Represents the largest possible value of a Byte. 此字段为常数。This field is constant.

MinValue MinValue MinValue MinValue

表示 Byte 的最小可能值。Represents the smallest possible value of a Byte. 此字段为常数。This field is constant.

方法

CompareTo(Byte) CompareTo(Byte) CompareTo(Byte) CompareTo(Byte)

将此实例与指定的 8 位无符号整数进行比较并返回对其相对值的指示。Compares this instance to a specified 8-bit unsigned integer and returns an indication of their relative values.

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

将此实例与指定对象进行比较并返回一个对二者的相对值的指示。Compares this instance to a specified object and returns an indication of their relative values.

Equals(Byte) Equals(Byte) Equals(Byte) Equals(Byte)

返回一个值,该值指示此实例和指定的 Byte 对象是否表示相同的值。Returns a value indicating whether this instance and a specified Byte object represent the same value.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否等于指定的对象。Returns a value indicating whether this instance is equal to a specified object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

GetTypeCode() GetTypeCode() GetTypeCode() GetTypeCode()

返回值类型 TypeCodeByteReturns the TypeCode for value type Byte.

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 ByteConverts the string representation of a number in a specified style and culture-specific format to its Byte equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效 ByteConverts the string representation of a number in a specified style to its Byte equivalent.

Parse(String) Parse(String) Parse(String) Parse(String)

将数字的字符串表示形式转换为它的等效 Byte 表示形式。Converts the string representation of a number to its Byte equivalent.

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

将指定的区域性特定格式的数字字符串表示形式转换为它的等效 ByteConverts the string representation of a number in a specified culture-specific format to its Byte equivalent.

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

使用指定的区域性特定格式设置信息将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the numeric value of the current Byte object to its equivalent string representation using the specified culture-specific formatting information.

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider)

使用指定的格式和区域性特定格式信息将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the value of the current Byte object to its equivalent string representation using the specified format and culture-specific formatting information.

ToString(String) ToString(String) ToString(String) ToString(String)

使用指定的格式将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the value of the current Byte object to its equivalent string representation using the specified format.

ToString() ToString() ToString() ToString()

将当前 Byte 对象的值转换为其等效的字符串表示形式。Converts the value of the current Byte object to its equivalent string representation.

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
TryParse(String, Byte) TryParse(String, Byte) TryParse(String, Byte) TryParse(String, Byte)

尝试将数字的字符串表示形式转换为它的等效 Byte,并返回一个指示转换是否成功的值。Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

TryParse(ReadOnlySpan<Char>, Byte) TryParse(ReadOnlySpan<Char>, Byte) TryParse(ReadOnlySpan<Char>, Byte) TryParse(ReadOnlySpan<Char>, Byte)
TryParse(String, NumberStyles, IFormatProvider, Byte) TryParse(String, NumberStyles, IFormatProvider, Byte) TryParse(String, NumberStyles, IFormatProvider, Byte) TryParse(String, NumberStyles, IFormatProvider, Byte)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 ByteConverts the string representation of a number in a specified style and culture-specific format to its Byte equivalent. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte)

显式界面实现

IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object)
IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.GetTypeCode()
IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider)

有关此成员的说明,请参阅 ToBoolean(IFormatProvider)For a description of this member, see ToBoolean(IFormatProvider).

IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider)

有关此成员的说明,请参阅 ToByte(IFormatProvider)For a description of this member, see ToByte(IFormatProvider).

IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider)

有关此成员的说明,请参阅 ToChar(IFormatProvider)For a description of this member, see ToChar(IFormatProvider).

IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider)

不支持此转换。This conversion is not supported. 尝试使用此方法将引发 InvalidCastExceptionAttempting to use this method throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider)

有关此成员的说明,请参阅 ToDecimal(IFormatProvider)For a description of this member, see ToDecimal(IFormatProvider).

IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider)

有关此成员的说明,请参阅 ToDouble(IFormatProvider)For a description of this member, see ToDouble(IFormatProvider).

IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider)

有关此成员的说明,请参阅 ToInt16(IFormatProvider)For a description of this member, see ToInt16(IFormatProvider).

IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider)

有关此成员的说明,请参阅 ToInt32(IFormatProvider)For a description of this member, see ToInt32(IFormatProvider).

IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider)

有关此成员的说明,请参阅 ToInt64(IFormatProvider)For a description of this member, see ToInt64(IFormatProvider).

IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider)

有关此成员的说明,请参阅 ToSByte(IFormatProvider)For a description of this member, see ToSByte(IFormatProvider).

IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider)

有关此成员的说明,请参阅 ToSingle(IFormatProvider)For a description of this member, see ToSingle(IFormatProvider).

IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider)

有关此成员的说明,请参阅 ToType(Type, IFormatProvider)For a description of this member, see ToType(Type, IFormatProvider).

IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider)

有关此成员的说明,请参阅 ToUInt16(IFormatProvider)For a description of this member, see ToUInt16(IFormatProvider).

IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider)

有关此成员的说明,请参阅 ToUInt32(IFormatProvider)For a description of this member, see ToUInt32(IFormatProvider).

IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider)

有关此成员的说明,请参阅 ToUInt64(IFormatProvider)For a description of this member, see ToUInt64(IFormatProvider).

Extension Methods

AsBuffer(Byte[]) AsBuffer(Byte[]) AsBuffer(Byte[]) AsBuffer(Byte[])
AsBuffer(Byte[], Int32, Int32) AsBuffer(Byte[], Int32, Int32) AsBuffer(Byte[], Int32, Int32) AsBuffer(Byte[], Int32, Int32)
AsBuffer(Byte[], Int32, Int32, Int32) AsBuffer(Byte[], Int32, Int32, Int32) AsBuffer(Byte[], Int32, Int32, Int32) AsBuffer(Byte[], Int32, Int32, Int32)
CopyTo(Byte[], Int32, IBuffer, UInt32, Int32) CopyTo(Byte[], Int32, IBuffer, UInt32, Int32) CopyTo(Byte[], Int32, IBuffer, UInt32, Int32) CopyTo(Byte[], Int32, IBuffer, UInt32, Int32)
CopyTo(Byte[], IBuffer) CopyTo(Byte[], IBuffer) CopyTo(Byte[], IBuffer) CopyTo(Byte[], IBuffer)

适用于

线程安全性

此类型所有成员都是线程安全。All members of this type are thread safe. 看起来要修改实例状态的成员实际上返回使用新值进行初始化的新实例。Members that appear to modify instance state actually return a new instance initialized with the new value. 作为使用任何其他类型,读取和写入对共享变量包含此类型的实例必须受到使用锁来确保线程安全性。As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

另请参阅