This topic has not yet been rated - Rate this topic

# Decimal Explicit Conversion (Decimal to Byte)

Silverlight

Converts a Decimal to an 8-bit unsigned integer.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
```public static explicit operator byte (
decimal value
)
```

#### Parameters

value
Type: System.Decimal
A Decimal to convert.

#### Return Value

Type: System.Byte
An 8-bit unsigned integer that represents the converted Decimal.
ExceptionCondition
OverflowException

value is less than Byte.MinValue or greater than Byte.MaxValue.

This operator supports the explicit conversion of a Decimal to a Byte. The syntax for such explicit conversions is language-dependent, and individual language compilers can provide different implementations and return different results. The example illustrates the different return values when you explicitly convert a Decimal value to a Byte value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the Decimal.ToByte or the Convert.ToByte(Decimal) method.

The following code example converts Decimal numbers to Byte values using the explicit Decimal to Byte conversion.

```
// Example of the explicit conversions from decimal to byte and
// decimal to sbyte.
using System;

class Example
{
const string formatter = "{0,16}{1,19}{2,19}";

// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType(Exception ex)
{
string exceptionType = ex.GetType().ToString();
return exceptionType.Substring(
exceptionType.LastIndexOf('.') + 1);
}

// Convert the decimal argument; catch exceptions that are thrown.
public static void DecimalToS_Byte(System.Windows.Controls.TextBlock outputBlock, decimal argument)
{
object SByteValue;
object ByteValue;

// Convert the argument to an sbyte value.
try
{
SByteValue = (sbyte)argument;
}
catch (Exception ex)
{
SByteValue = GetExceptionType(ex);
}

// Convert the argument to a byte value.
try
{
ByteValue = (byte)argument;
}
catch (Exception ex)
{
ByteValue = GetExceptionType(ex);
}

outputBlock.Text += String.Format(formatter, argument,
SByteValue, ByteValue) + "\n";
}

public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text += String.Format(
"This example of the explicit conversions from decimal " +
"to sbyte \nand decimal to byte generates the following " +
"output. It displays \nseveral converted decimal " +
"values.\n") + "\n";
outputBlock.Text += String.Format(formatter, "decimal argument",
"sbyte/exception", "byte/exception") + "\n";
outputBlock.Text += String.Format(formatter, "----------------",
"---------------", "--------------") + "\n";

// Convert decimal values and display the results.
DecimalToS_Byte(outputBlock, 78M);
DecimalToS_Byte(outputBlock, new decimal(78000, 0, 0, false, 3));
DecimalToS_Byte(outputBlock, 78.999M);
DecimalToS_Byte(outputBlock, 255.999M);
DecimalToS_Byte(outputBlock, 256M);
DecimalToS_Byte(outputBlock, 127.999M);
DecimalToS_Byte(outputBlock, 128M);
DecimalToS_Byte(outputBlock, -0.999M);
DecimalToS_Byte(outputBlock, -1M);
DecimalToS_Byte(outputBlock, -128.999M);
DecimalToS_Byte(outputBlock, -129M);
}
}

/*
This example of the explicit conversions from decimal to sbyte
and decimal to byte generates the following output. It displays
several converted decimal values.

decimal argument    sbyte/exception     byte/exception
----------------    ---------------     --------------
78                 78                 78
78.000                 78                 78
78.999                 78                 78
255.999  OverflowException                255
256  OverflowException  OverflowException
127.999                127                127
128  OverflowException                128
-0.999                  0                  0
-1                 -1  OverflowException
-128.999               -128  OverflowException
-129  OverflowException  OverflowException
*/

```

#### Silverlight

Supported in: 5, 4, 3

#### Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

#### XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.