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. 如果非字节值是Single包含小数部分的、 DoubleDecimal值,则其小数部分的处理取决于执行转换的编译器。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. 字符串可以包含 decimal 或十六进制数字。The string can contain either decimal or hexadecimal digits. 下面的示例通过使用 decimal 和十六进制字符串说明了分析操作。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值,也可以CompareTo调用或Equals方法。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      

还可以通过ToString(Byte, Int32)调用方法Byte并提供作为方法的第二个参数的基数,将值的格式设置为二进制、八进制、十进制或十六进制字符串。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. 下面的示例执行此来掩盖8位有符号和无符号值数组的位2和4。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).

适用于

线程安全性

此类型的所有成员都是线程安全的。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.

另请参阅