This documentation is archived and is not being maintained.

Decimal.Explicit Operator

Visual Studio 2008

Converts a Decimal to a 16-bit unsigned integer.

This API is not CLS-compliant.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

```public static explicit operator ushort (
decimal value
)
```

Parameters

value
Type: System.Decimal

A Decimal to convert.

Return Value

Type: System.UInt16
A 16-bit unsigned integer that represents the converted Decimal.

Exceptions

ExceptionCondition
OverflowException

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

Remarks

This operator supports the explicit conversion of a Decimal to a UInt16. 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 UInt16 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToUInt16 or the Convert.ToUInt16(Decimal) method.

Examples

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

```// Example of the explicit conversions from decimal to short and
// decimal to ushort.
using System;

class DecimalToU_Int16Demo
{
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 DecimalToU_Int16( decimal argument )
{
object Int16Value;
object UInt16Value;

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

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

Console.WriteLine( formatter, argument,
Int16Value, UInt16Value );
}

public static void Main( )
{
Console.WriteLine(
"This example of the explicit conversions from decimal " +
"to short \nand decimal to ushort generates the " +
"following output. It displays \nseveral converted " +
"decimal values.\n" );
Console.WriteLine( formatter, "decimal argument",
"short/exception", "ushort/exception" );
Console.WriteLine( formatter, "----------------",
"---------------", "----------------" );

// Convert decimal values and display the results.
DecimalToU_Int16( 123M );
DecimalToU_Int16( new decimal( 123000, 0, 0, false, 3 ) );
DecimalToU_Int16( 123.999M );
DecimalToU_Int16( 65535.999M );
DecimalToU_Int16( 65536M );
DecimalToU_Int16( 32767.999M );
DecimalToU_Int16( 32768M );
DecimalToU_Int16( - 0.999M );
DecimalToU_Int16( - 1M );
DecimalToU_Int16( - 32768.999M );
DecimalToU_Int16( - 32769M );
}
}

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

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
123                123                123
123.000                123                123
123.999                123                123
65535.999  OverflowException              65535
65536  OverflowException  OverflowException
32767.999              32767              32767
32768  OverflowException              32768
-0.999                  0                  0
-1                 -1  OverflowException
-32768.999             -32768  OverflowException
-32769  OverflowException  OverflowException
*/
```

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0