Mono Class Library: System.Convert Overview | Members

System.Convert.ToByte Method

Converts a ushort to a byte . [Edit]

[System.CLSCompliant(false)]
public static byte ToByte (ushort value)

Parameters

value
The 16-bit unsigned integer value to be converted. [Edit]

Returns

value as a byte. [Edit]

Exceptions

TypeReason
OverflowExceptionvalue is greater than byte.MaxValue. [Edit]

Remarks

This member is not CLS-compliant. For a CLS-compliant alternative, use Convert.ToByte(object)(int). [Edit]

Example

The following example demonstrates converting ushort values to byte values.

C# Example
using System;
class ConvertByteTest {
    static public void Main() {
        ushort ushort0 = 0;
        ushort ushort1 = 32000;
        byte byte0 = Convert.ToByte(ushort0);
        Console.WriteLine("(ushort) {0} as byte = {1}",ushort0,byte0);
        byte byte1 = Convert.ToByte(ushort1); //Throws an exception.
        Console.WriteLine("(ushort) {0} as byte = {1}",ushort1,byte1);
    }
}

The output is

(ushort) 0 as byte = 0
Exception occurred: System.OverflowException: Value was either too large or too small for an unsigned byte.
at Convert.ToByte(UInt16 value)
at ConvertByteTest.Main() in C:\ECMAExamples\ConvertToByte\ConvertUInt16.cs:line 8

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0