BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 This type is safe for multithreaded operations. System.ValueType System.IComparable System.IComparable<System.Byte> System.IConvertible System.IEquatable<System.Byte> System.IFormattable System.Runtime.InteropServices.ComVisible(true)

is an immutable value type that represents unsigned integers with values that range from 0 (which is represented by the constant) to 255 (which is represented by the constant). The .NET Framework also includes a signed 8-bit integer value type, , which represents values that range from -128 to 127.

## Instantiating a Byte Value

You can instantiate a value in several ways:

You can declare a variable and assign it a literal integer value that is within the range of the data type. The following example declares two variables and assigns them values in this way.

code reference: System.Byte.Instantiation#1

You can assign a non-byte numeric value to a byte. 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. If the non-byte value is a , , or value that includes a fractional component, the handling of its fractional part depends on the compiler performing the conversion. The following example assigns several numeric values to variables.

code reference: System.Byte.Instantiation#2

You can call a method of the class to convert any supported type to a value. This is possible because supports the interface. The following example illustrates the conversion of an array of values to values.

code reference: System.Convert.ToByte#4

You can call the or method to convert the string representation of a value to a . 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.

code reference: System.Byte.Instantiation#3

## Performing Operations on Byte Values

The type supports standard mathematical operations such as addition, subtraction, division, multiplication, subtraction, negation, and unary negation. Like the other integral types, the type also supports the bitwise AND, OR, XOR, left shift, and right shift operators.

You can use the standard numeric operators to compare two values, or you can call the or method.

You can also call the members of the 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

The 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.

To format a value as an integral string with no leading zeros, you can call the parameterless method. By using the "D" format specifier, you can also include a specified number of leading zeros in the string representation. By using the "X" format specifier, you can represent a value as a hexadecimal string. The following example formats the elements in an array of values in these three ways.

code reference: System.Byte.Formatting#1

You can also format a value as a binary, octal, decimal, or hexadecimal string by calling the 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.

code reference: System.Byte.Formatting#2

## 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. For example, overloads of the method can convert each of the primitive data types to a byte array, and the method converts a value to a byte array.

values are represented in 8 bits by their magnitude only, without a sign bit. This is important to keep in mind when you perform bitwise operations on 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.

When an operation is performed on two values, the values share the same representation, so the result is accurate. This is illustrated in the following example, which masks the lowest-order bit of a value to ensure that it is even.

code reference: System.Byte.Bitwise#1

On the other hand, when you work with both unsigned and signed bits, bitwise operations are complicated by the fact that the 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. The following example does this to mask out bits 2 and 4 of an array of 8-bit signed and unsigned values.

code reference: System.Byte.Bitwise#2

Represents an 8-bit unsigned integer.

- Method
  2.0.0.0
  4.0.0.0
  System.Int32 ``

  This method implements the interface and performs slightly better than the method because it does not have to convert the parameter to an object.

  Depending on your programming language, it might be possible to code a method where the parameter type has fewer bits (is narrower) than the instance type. This is possible because some programming languages perform an implicit widening conversion that represents the parameter as a type with as many bits as the instance.

  For example, suppose the instance type is and the parameter type is . The Microsoft C# compiler generates instructions to represent the value of the parameter as an , then generates a method that compares the values of the instance and the parameter representation.

  Consult your programming language's documentation to determine whether its compiler performs implicit widening conversions on numeric types.

  Compares this instance to a specified 8-bit unsigned integer and returns an indication of their relative values.

  A signed integer that indicates the relative order of this instance and .

  Return Value

  Description

  Less than zero

  This instance is less than .

  Zero

  This instance is equal to .

  Greater than zero

  This instance is greater than .

  An 8-bit unsigned integer to compare.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32 `` is not a and is not a null reference.

  must be null or an instance of ; otherwise, an exception is thrown.

  Any instance of , regardless of its value, is considered greater than null.

  This method is implemented to support the interface.

  Compares this instance to a specified object and returns an indication of their relative values.

  A signed integer that indicates the relative order of this instance and .

  Return Value

  Description

  Less than zero

  This instance is less than .

  Zero

  This instance is equal to .

  Greater than zero

  This instance is greater than .

  -or-

  is null.

  An object to compare, or null. 0
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  This method implements the interface, and performs slightly better than because it does not have to convert the parameter to an object.

  Returns a value indicating whether this instance and a specified object represent the same value.

  true if is equal to this instance; otherwise, false.

  An object to compare to this instance.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  This method overrides .

  An object to compare with this instance, or null.

  Returns a value indicating whether this instance is equal to a specified object.

  true if is an instance of and equals the value of this instance; otherwise, false.

  An object to compare with this instance, or null. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32

  The algorithm used to generate the hash code is unspecified.

  This method overrides .

  Returns the hash code for this instance.

  A hash code for the current .

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.TypeCode To be added.

  Returns the for value type .

  The enumerated constant, .

- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte 255

  The value of this constant is 255 (hexadecimal 0xFF).

  Represents the largest possible value of a . This field is constant.

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte 0

  The value of this constant is 0.

  Represents the smallest possible value of a . This field is constant.

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte `` is a null reference. is not in the correct style. represents a number greater than or less than .

  The following example demonstrates the method.

  `using System; public class ByteParseClass { public static void Main() { string str = " 100 "; Console.WriteLine("String: \"{0}\" <Byte> {1}",str,Byte.Parse(str)); } }`

  The output is

  String: " 100 " <Byte> 100

  The parameter contains a number of the form:

  [ws][sign]digits[ws]

  Elements in square brackets ([ and ]) are optional. The following table describes each element.

  Element

  Description

  ws

  Optional white space.

  sign

  An optional positive or negative sign.

  digits

  A sequence of digits ranging from 0 to 9.

  The parameter is interpreted using the style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an .) To explicitly define the style elements that can be present in , use either the or the method.

  The parameter is parsed using the formatting information in a object that is initialized for the current system culture. For more information, see . To parse a string using the formatting information of some other culture, use the method.

  Converts the string representation of a number to its equivalent.

  A byte value that is equivalent to the number contained in .

  A string that contains a number to convert. The string is interpreted using the style. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte `` `` is a null reference. is not in the correct style. represents a number greater than or less than . A string that contains a number to convert. The string is interpreted using the style specified by . A bitwise combination of enumeration values that indicates the style elements that can be present in . A typical value to specify is .

  The parameter defines the style elements (such as white space or the positive sign) that are allowed in the parameter for the parse operation to succeed. It must be a combination of bit flags from the enumeration. Depending on the value of , the parameter may include the following elements:

  [ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

  Or, if includes :

  [ws]hexdigits[ws]

  Elements in square brackets ([ and ]) are optional. The following table describes each element.

  Element

  Description

  ws

  Optional white space. White space can appear at the beginning of if includes the flag, or at the end of s if style includes the flag.

  $

  A culture-specific currency symbol. Its position in the string is defined by the property of the current culture. The current culture's currency symbol can appear in if includes the flag.

  sign

  An optional positive sign. (The method throws an if a negative sign is present in .) The sign can appear at the beginning of if includes the flag, or at the end of if includes the flag.

  digits

  A sequence of digits from 0 through 9.

  .

  A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in if includes the flag.

  fractional_digits

  One or more occurrences of the digit 0. Fractional digits can appear in only if includes the flag.

  e

  The e or E character, which indicates that the value is represented in exponential notation. The parameter can represent a number in exponential notation if includes the flag.

  hexdigits

  A sequence of hexadecimal digits from 0 through f, or 0 through F.

  A string with decimal digits only (which corresponds to the style) always parses successfully. Most of the remaining members control elements that may be but are not required to be present in this input string. The following table indicates how individual members affect the elements that may be present in .

  Non-composite NumberStyles values

  Elements permitted in s in addition to digits

  Decimal digits only.

  The . and fractional_digits elements. However, fractional_digits must consist of only one or more 0 digits or an is thrown.

  The parameter can also use exponential notation.

  The ws element at the beginning of .

  The ws element at the end of .

  A positive sign can appear before digits.

  A positive sign can appear after digits.

  Although this flag is supported, the use of parentheses in results in an .

  Although the group separator symbol can appear in , it can be preceded by only one or more 0 digits.

  The $ element.

  If the flag is used, must be a hexadecimal value. The only other flags that can be combined with it are and . (The enumeration includes a composite number style, , that includes both white space flags.)

  The parameter is parsed using the formatting information in a object that is initialized for the current system culture. To use the formatting information of some other culture, call the overload.

  Converts the string representation of a number in a specified style to its equivalent.

  A byte value that is equivalent to the number contained in .

  A string that contains a number to convert. The string is interpreted using the style specified by . A bitwise combination of enumeration values that indicates the style elements that can be present in . A typical value to specify is . 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte `` `` is a null reference. represents a number greater than or less than . is not in the correct style. A string that contains a number to convert. The string is interpreted using the style. An object that supplies culture-specific parsing information about . If is null, the thread current culture is used.

  The parameter contains a number of the form:

  [ws][sign]digits[ws]

  Elements in square brackets ([ and ]) are optional. The following table describes each element.

  Element

  Description

  ws

  Optional white space.

  sign

  An optional positive sign.

  digits

  A sequence of digits ranging from 0 to 9.

  The parameter is interpreted using the style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an .) To explicitly define the style elements together with the culture-specific formatting information that can be present in , use the method.

  The parameter is parsed using the formatting information in a object supplied by . The parameter is an implementation such as a or object. The parameter supplies culture-specific information used in parsing. If is null, the thread current culture is used.

  Converts the string representation of a number in a specified culture-specific format to its equivalent.

  A byte value that is equivalent to the number contained in .

  A string that contains a number to convert. The string is interpreted using the style. An object that supplies culture-specific parsing information about . If is null, the thread current culture is used. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte `` `` `` is a null reference. is not in the correct style. represents a number greater than or less than . A string that contains a number to convert. The string is interpreted using the style specified by . A bitwise combination of enumeration values that indicates the style elements that can be present in . A typical value to specify is . An object that supplies culture-specific information about the format of . If is null, the thread current culture is used.

  The parameter defines the style elements (such as white space or the positive sign) that are allowed in the parameter for the parse operation to succeed. It must be a combination of bit flags from the enumeration. Depending on the value of , the parameter may include the following elements:

  [ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

  Or, if the parameter includes :

  [ws]hexdigits[ws]

  Elements in square brackets ([ and ]) are optional. The following table describes each element.

  Element

  Description

  ws

  Optional white space. White space can appear at the beginning of if includes the flag, or at the end of if includes the flag.

  $

  A culture-specific currency symbol. Its position in the string is defined by the property of the object returned by the method of the parameter. The currency symbol can appear in if includes the flag.

  sign

  An optional positive sign. (The method throws an if a negative sign is present in .) The sign can appear at the beginning of if includes the flag, or at the end of if includes the flag.

  digits

  A sequence of digits from 0 through 9.

  .

  A culture-specific decimal point symbol. The decimal point symbol of the culture specified by can appear in if includes the flag.

  fractional_digits

  One or more occurrences of the digit 0. Fractional digits can appear in only if includes the flag.

  e

  The e or E character, which indicates that the value is represented in exponential notation. The s parameter can represent a number in exponential notation if includes the flag.

  hexdigits

  A sequence of hexadecimal digits from 0 through f, or 0 through F.

  A string with decimal digits only (which corresponds to the style) always parses successfully. Most of the remaining members control elements that may be but are not required to be present in this input string. The following table indicates how individual members affect the elements that may be present in .

  Non-composite NumberStyles values

  Elements permitted in s in addition to digits

  Decimal digits only.

  The . and fractional_digits elements. However, fractional_digits must consist of only one or more 0 digits or an is thrown.

  The parameter can also use exponential notation.

  The ws element at the beginning of .

  The ws element at the end of .

  A positive sign can appear before digits.

  A positive sign can appear after digits.

  Although this flag is supported, the use of parentheses in results in an .

  Although the group separator symbol can appear in , it can be preceded by only one or more 0 digits.

  The $ element.

  If the flag is used, must be a hexadecimal value. The only other flags that can be present in are and . (The enumeration has a composite number style, , that includes both white space flags.)

  The parameter is an implementation, such as a or object. The parameter supplies culture-specific information used in parsing. If is null, the thread current culture is used.

  Converts the string representation of a number in a specified style and culture-specific format to its equivalent.

  A byte value that is equivalent to the number contained in .

  A string that contains a number to convert. The string is interpreted using the style specified by . A bitwise combination of enumeration values that indicates the style elements that can be present in . A typical value to specify is . An object that supplies culture-specific information about the format of . If is null, the thread current culture is used. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  true if the value of the current instance is not zero; otherwise, false.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface.

  For a description of this member, see .

  The value of the current instance, unchanged.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Char ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.DateTime `` To be added.

  This conversion is not supported. Attempting to use this method throws an .

  This conversion is not supported. No value is returned.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Decimal ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Double ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int16 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to an .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to an .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int64 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to an .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.SByte ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to an .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Single ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Object `` `` To be added.

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the static (Shared in Visual Basic) method instead.

  For a description of this member, see .

  The value of the current instance, converted to .

  An implementation that supplies information about the format of the returned value.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.UInt16 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.UInt32 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.UInt64 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  The value of the current instance, converted to a .

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String

  The return value is formatted with the general numeric format specifier ("G") and the object for the thread current culture. To define the formatting of the value's string representation, call the method. To define both the format specifiers and culture used to create the string representation of a value, call the method.

  The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

  For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

  For more information about formatting, see Formatting Types.

  For information about the thread current culture, see .

  Converts the value of the current object to its equivalent string representation.

  The string representation of the value of this object, which consists of a sequence of digits that range from 0 to 9 with no leading zeroes.

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String ``

  The return value is formatted with the general numeric format specifier ("G").

  The parameter is an object that implements the interface. Its method returns a object that provides culture-specific information about the format of the string that is returned by this method. The object that implements can be any of the following:

  A object that represents the culture whose formatting rules are to be used.

  A object that contains specific numeric formatting information for this value.

  A custom object that implements .

  If is null or a object cannot be obtained from , the return value is formatted using the object for the thread current culture. For information about the thread current culture, see .

  The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

  For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

  For more information about formatting, see Formatting Types.

  Converts the numeric value of the current object to its equivalent string representation using the specified culture-specific formatting information.

  The string representation of the value of this object in the format specified by the parameter.

  An object that supplies culture-specific formatting information. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String `` is invalid.

  The following example demonstrates the method.

  `using System; public class ByteToStringExample { public static void Main() { Byte b = 8; Console.WriteLine(b); String[] formats = {"c", "d", "e", "f", "g", "n", "p", "x" }; foreach(String str in formats) Console.WriteLine("{0}: {1}", str, b.ToString(str)); } }`

  The output is

  8

  c: $8.00

  d: 8

  e: 8.000000e+000

  f: 8.00

  g: 8

  n: 8.00

  p: 800.00 %

  x: 8

  The parameter can be either a standard or a custom numeric format string. All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. If is null or an empty string (""), the return value is formatted with the general numeric format specifier ("G").

  The return value of this function is formatted using the object for the thread current culture. For information about the thread current culture, see . To provide formatting information for cultures other than the current culture, call the method.

  The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

  For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

  For more information about formatting, see Formatting Types.

  Converts the value of the current object to its equivalent string representation using the specified format.

  The string representation of the current object, formatted as specified by the parameter.

  A numeric format string. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String `` `` is invalid. A standard or custom numeric format string. An object that supplies culture-specific formatting information.

  The method formats a value in a specified format of a specified culture. To format a number by using the default ("G") format of the current culture, call the method. To format a number by using a specified format of the current culture, call the method.

  The parameter can be either a standard or a custom numeric format string. All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. If is null or an empty string (""), the return value of this method is formatted with the general numeric format specifier ("G").

  The parameter is an object that implements the interface. Its method returns a object that provides culture-specific information about the format of the string that is returned by this method. The object that implements can be any of the following:

  A object that represents the culture whose formatting rules are to be used.

  A object that contains specific numeric formatting information for this value.

  A custom object that implements .

  If is null or a object cannot be obtained from , the return value is formatted using the object for the thread current culture. For information about the thread current culture, see .

  The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

  For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

  For more information about formatting, see Formatting Types.

  Converts the value of the current object to its equivalent string representation using the specified format and culture-specific formatting information.

  The string representation of the current object, formatted as specified by the and parameters.

  A standard or custom numeric format string. An object that supplies culture-specific formatting information. 0
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` To be added. To be added. To be added. To be added. To be added.
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. To be added.
0