System.Byte Structure

Assembly: Mscorlib.dll
Namespace: System
Summary
Represents an 8-bit unsigned integer.
C# Syntax:
[Serializable]
public struct Byte : IComparable, IFormattable, IConvertible
Thread Safety
This type is safe for multithreaded operations.
Remarks
The Byte value type represents unsigned integers with values ranging from 0 to 255.

Byte provides methods to compare instances of this type, convert the value of an instance to its string representation, and convert the string representation of a number to an instance of this type.

For information about how format specification codes control the string representation of value types, see the conceptual topic at MSDN: formattingoverview.

This type implements interfaces IComparable, IFormattable, and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.

This type is thread safe; multiple threads can concurrently read from an instance of this type.

See also:
System Namespace | SByte | String | IComparable | IFormattable | IConvertible

System.Byte Member List:

Public Fields
MaxValue Represents the largest possible value of a Byte. This field is constant.
MinValue Represents the smallest possible value of a Byte. This field is constant.
Public Methods
CompareTo Compares this instance to a specified object and returns an indication of their relative values.
Equals Overridden:
Returns a value indicating whether this instance is equal to a specified object.
GetHashCode Overridden:
Returns the hash code for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
GetTypeCode Returns the TypeCode for value type Byte.
Parse Overloaded:
Parse(string s)

Converts the string representation of a number to its Byte equivalent.
Parse Overloaded:
Parse(string s, IFormatProvider provider)

Converts the string representation of a number in a specified culture-specific format to its Byte equivalent.
Parse Overloaded:
Parse(string s, NumberStyles style)

Converts the string representation of a number in a specified style to its Byte equivalent.
Parse Overloaded:
Parse(string s, NumberStyles style, IFormatProvider provider)

Converts the string representation of a number in a specified style and culture-specific format to its Byte equivalent.
ToString Overloaded:
ToString()

Overridden:
Converts the numeric value of this instance to its equivalent string.
ToString Overloaded:
ToString(IFormatProvider provider)

Converts the numeric value of this instance to its equivalent string using the specified culture-specific format information.
ToString Overloaded:
ToString(string format)

Converts the numeric value of this instance to its equivalent string using the specified format.
ToString Overloaded:
ToString(string format, IFormatProvider provider)

Converts the numeric value of this instance to its equivalent string using the specified format and culture-specific format information.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Byte Member Details

Field: MaxValue
Summary
Represents the largest possible value of a Byte. This field is constant.
C# Syntax:
public const byte MaxValue;
Remarks
The value of this constant is 255 (hexadecimal 0xFF).
Example
      public void MinMaxFields(int numberToSet)
      {
         if(numberToSet <= (int)Byte.MaxValue && numberToSet >= (int)Byte.MinValue)
         {
            // You must explicitly convert an integer to a byte.
            MemberByte = (Byte)numberToSet;

            // Displays MemberByte using the ToString() method.
            Console.WriteLine("The MemberByte value is {0}", MemberByte.ToString());
         }
         else
         {
            Console.WriteLine("The value {0} is outside of the range of possible Byte values", numberToSet.ToString());
         }
      }

    
See also:
Byte.MinValue

Return to top


Field: MinValue
Summary
Represents the smallest possible value of a Byte. This field is constant.
C# Syntax:
public const byte MinValue;
Remarks
The value of this constant is 0.
Example
      public void MinMaxFields(int numberToSet)
      {
         if(numberToSet <= (int)Byte.MaxValue && numberToSet >= (int)Byte.MinValue)
         {
            // You must explicitly convert an integer to a byte.
            MemberByte = (Byte)numberToSet;

            // Displays MemberByte using the ToString() method.
            Console.WriteLine("The MemberByte value is {0}", MemberByte.ToString());
         }
         else
         {
            Console.WriteLine("The value {0} is outside of the range of possible Byte values", numberToSet.ToString());
         }
      }

    
See also:
Byte.MaxValue

Return to top


Method: CompareTo(
   object value
)
Summary
Compares this instance to a specified object and returns an indication of their relative values.
C# Syntax:
public int CompareTo(
   object value
);
Parameters:

value

An object to compare, or null.

Return Value:
A signed integer that indicates the relative order of this instance and value.

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 value. -or- value is null.
Exceptions
Exception Type Condition
ArgumentException value is not a Byte or null.
Remarks
value must be null or an instance of Byte; otherwise, an exception is thrown.

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

This method is implemented to support the IComparable interface.

Example
      public void Compare(Byte myByte)
      {
         int myCompareResult;
  
         myCompareResult = MemberByte.CompareTo(myByte);

         if(myCompareResult > 0)
         {
            Console.WriteLine("{0} is less than the MemberByte value {1}", myByte.ToString(), MemberByte.ToString());
         }
         else if(myCompareResult < 0)
         {
            Console.WriteLine("{0} is greater than the MemberByte value {1}", myByte.ToString(), MemberByte.ToString());
         }
         else
         {
            Console.WriteLine("{0} is equal to the MemberByte value {1}", myByte.ToString(), MemberByte.ToString());
         }
      }

    
See also:
ArgumentException | IComparable | Byte.Equals

Return to top


Overridden Method: Equals(
   object obj
)
Summary
Returns a value indicating whether this instance is equal to a specified object.
C# Syntax:
public override bool Equals(
   object obj
);
Parameters:

obj

An object to compare with this instance, or null.

Return Value:
true if obj is an instance of Byte and equals the value of this instance; otherwise, false.
See also:
Byte.CompareTo

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Byte();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Returns the hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
A hash code for the current Byte.
See also:
Object.GetHashCode

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetTypeCode()
Summary
Returns the TypeCode for value type Byte.
C# Syntax:
public TypeCode GetTypeCode();
Return Value:
The enumerated constant, TypeCode.Byte.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Parse(
   string s
)
Summary
Converts the string representation of a number to its Byte equivalent.
C# Syntax:
public static byte Parse(
   string s
);
Parameters:

s

A string containinga number to convert. The string is interpreted using the NumberStyles.Integer style.

Return Value:
The Byte value equivalent to the number contained in s.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s is not of the correct format.
OverflowException s represents a number less than Byte.MinValue or greater than Byte.MaxValue.
Remarks
The s parameter is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Items in square brackets ('[' and ']') are optional, and other items are as follows. ws Optional white space. sign An optional positive or negative sign. digits A sequence of digits ranging from 0 to 9.
Example
      public void ParseByte(String stringToConvert)
      {
         try
         {
            MemberByte = Byte.Parse(stringToConvert);
            Console.WriteLine("The MemberByte value is {0}", MemberByte.ToString());
         }
         catch(System.OverflowException e)
         {
            Console.WriteLine("Exception: {0}", e.Message);
         }
      }

    
See also:
MSDN: formattingoverview | Byte.ToString | Byte.MaxValue | Byte.MinValue | NumberStyles | NumberFormatInfo

Return to top


Overloaded Method: Parse(
   string s,
   IFormatProvider provider
)
Summary
Converts the string representation of a number in a specified culture-specific format to its Byte equivalent.
C# Syntax:
public static byte Parse(
   string s,
   IFormatProvider provider
);
Parameters:

s

A string containing a number to convert. The string is interpretedusing the NumberStyles.Integer style.

provider

An IFormatProvider that supplies culture-specific formatting information about s. If provider is null, the current system culture is used.

Return Value:
The Byte value equivalent to the number contained in s.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s is not of the correct format.
OverflowException s represents a number less than Byte.MinValue or greater than Byte.MaxValue.
Remarks
The s parameter is parsed using the formatting information in a NumberFormatInfo supplied by provider.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Items in square brackets ('[' and ']') are optional, and other items are as follows.

ws Optional white space. sign An optional positive sign. digits A sequence of digits ranging from 0 to 9.

The provider parameter is an IFormatProvider instance that supplies a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about the format of s.

See also:
MSDN: formattingoverview | Byte.ToString | Byte.MaxValue | Byte.MinValue | NumberStyles | NumberFormatInfo | IFormatProvider

Return to top


Overloaded Method: Parse(
   string s,
   NumberStyles style
)
Summary
Converts the string representation of a number in a specified style to its Byte equivalent.
C# Syntax:
public static byte Parse(
   string s,
   NumberStyles style
);
Parameters:

s

A string containing a number to convert. The string is interpreted using the style specified by style.

style

A bitwise combination of NumberStyles values that indicate the permitted format of s. If style is null, the string is interpreted using the NumberStyles.Integer style.

Return Value:
The Byte value equivalent to the number contained in s.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s is not of the correct format.
OverflowException s represents a number less than Byte.MinValue or greater than Byte.MaxValue.
ArgumentException style is not a valid bitwise combination of NumberStyles values.
Remarks
The s parameter is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Items in square brackets ('[' and ']') are optional, and other items are as follows.

ws Optional white space. sign An optional positive sign. digits A sequence of digits ranging from 0 to 9.

The style parameter is a bitwise combination of zero or more NumberStyles values, except NumberStyles.Any and NumberStyles.AllowDecimalPoint, which are invalid for the type returned by this method.

See also:
MSDN: formattingoverview | Byte.ToString | Byte.MaxValue | Byte.MinValue | NumberStyles | NumberFormatInfo | IFormatProvider

Return to top


Overloaded Method: Parse(
   string s,
   NumberStyles style,
   IFormatProvider provider
)
Summary
Converts the string representation of a number in a specified style and culture-specific format to its Byte equivalent.
C# Syntax:
public static byte Parse(
   string s,
   NumberStyles style,
   IFormatProvider provider
);
Parameters:

s

A string containinga number to convert. The string is interpreted using the style specified by style.

style

A bitwise combination of NumberStyles values that indicate the permitted format of s. If style is null, the string is interpreted using the NumberStyles.Integer style.

provider

An IFormatProvider that supplies culture-specific formatting information about s. If provider is null, the current system culture is used.

Return Value:
The Byte value equivalent to the number contained in s.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s is not of the correct format.
OverflowException s represents a number less than Byte.MinValue or greater than Byte.MaxValue.
ArgumentException style is not a valid bitwise combination of NumberStyles values.
Remarks
The s parameter is parsed using the formatting information in a NumberFormatInfo supplied by provider.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Items in square brackets ('[' and ']') are optional, and other items are as follows.

ws Optional white space. sign An optional positive sign. digits A sequence of digits ranging from 0 to 9.

The style parameter is a bitwise combination of zero or more NumberStyles values, except NumberStyles.Any and NumberStyles.AllowDecimalPoint, which are invalid for the type returned by this method.

The provider parameter is an IFormatProvider instance that supplies a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about the format of s.

See also:
MSDN: formattingoverview | Byte.ToString | Byte.MaxValue | Byte.MinValue | NumberStyles | NumberFormatInfo | IFormatProvider

Return to top


Overloaded Method: ToString()
Summary
Converts the numeric value of this instance to its equivalent string.
C# Syntax:
public override string ToString();
Return Value:
The value of this instance.
Remarks
The return value is formatted with the general format specifier ("G") and the NumberFormatInfo for the current culture.
See also:
MSDN: formattingoverview | Byte.Parse | String | NumberFormatInfo

Return to top


Overloaded Method: ToString(
   IFormatProvider provider
)
Summary
Converts the numeric value of this instance to its equivalent string using the specified culture-specific format information.
C# Syntax:
public string ToString(
   IFormatProvider provider
);
Parameters:

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value:
The value of this instance, formatted as specified by provider.
Remarks
This instance is formatted with the general format specifier ("G").

The provider parameter is an IFormatProvider instance that supplies a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about this instance. If provider is null or a NumberFormatInfo cannot be obtained from provider, this instance is formatted with the NumberFormatInfo for the current system culture.

See also:
MSDN: formattingoverview | Byte.Parse | String | NumberFormatInfo | IFormatProvider

Return to top


Overloaded Method: ToString(
   string format
)
Summary
Converts the numeric value of this instance to its equivalent string using the specified format.
C# Syntax:
public string ToString(
   string format
);
Parameters:

format

A string thatspecifies the return format. See Byte.ToString for a list of valid values.

Return Value:
The value of this instance, formatted as specified by format.
Remarks
If format is null or an empty string (""), the return value of this instance is formatted with the general format specifier ("G").

The return value of this instance is formatted with the NumberFormatInfo for the current culture.

See also:
MSDN: formattingoverview | Byte.Parse | String | NumberFormatInfo

Return to top


Overloaded Method: ToString(
   string format,
   IFormatProvider provider
)
Summary
Converts the numeric value of this instance to its equivalent string using the specified format and culture-specific format information.
C# Syntax:
public string ToString(
   string format,
   IFormatProvider provider
);
Parameters:

format

A string thatspecifies the return format.

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value:
The value of this instance, formatted as specified by format and provider.
Remarks
If format is null or an empty string (""), the return value of this instance is formatted with the general format specifier ("G").

The following table lists the format characters that are valid for Byte.



Format Characters Description
"C", "c" Currency format.
"D", "d" Decimal format.
"E", "e" Exponential notation format.
"F", "f" Fixed-point format.
"G", "g" General format.
"N", "n" Number format.
"P", "p" Percent format.
"X", "x" Hexadecimal format.

The provider parameter is an IFormatProvider instance that supplies a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about this instance. If provider is null or a NumberFormatInfo cannot be obtained from provider, this instance is formatted with the NumberFormatInfo for the current system culture.

For a detailed description of formatting, see the IFormattable interface. This method is implemented to support the IFormattable interface.

See also:
MSDN: formattingoverview | Byte.Parse | String | NumberFormatInfo | IFormattable | IFormatProvider

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.