- value
- The 16-bit unsigned integer value to be converted.
value as a byte.
Type Reason OverflowException value is greater than byte.MaxValue.
This member is not CLS-compliant. For a CLS-compliant alternative, use Convert.ToByte(object)(int).
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
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0