Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
UInt16 Structure
UInt16 Fields
 MaxValue Field

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
UInt16..::.MaxValue Field

Represents the largest possible value of UInt16. This field is constant.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Const MaxValue As UShort
Visual Basic (Usage)
Dim value As UShort

value = UShort.MaxValue
C#
public const ushort MaxValue
Visual C++
public:
literal unsigned short MaxValue
JScript
public const var MaxValue : ushort

The value of this constant is 65535; that is, hexadecimal 0xFFFF.

The following example uses the MaxValue and MinValue properties to ensure that an Int32 value is in the range of the UInt16 type before converting it to a UInt16 value. This prevents the conversion operation from throwing an OverflowException if the integer value is not in the range of the UInt16 type.

Visual Basic
Dim integerValue As Integer = 1216
Dim uIntegerValue As UShort

If integerValue >= UShort.MinValue And integerValue <= UShort.MaxValue Then
   uIntegerValue = CUShort(integerValue) 
   Console.WriteLine(uIntegerValue)
Else
   Console.WriteLine("Unable to convert {0} to a UInt16t.", integerValue)   
End If   

C#
int integerValue = 1216; 
ushort uIntegerValue;

if (integerValue >= ushort.MinValue & integerValue <= ushort.MaxValue)
{
   uIntegerValue = (ushort) integerValue;
   Console.WriteLine(uIntegerValue);
} 
else
{
   Console.WriteLine("Unable to convert {0} to a UInt16t.", integerValue);
}     

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.

.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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Uint Maxvalue/MinValue sampe using PowerShell      Thomas Lee   |   Edit   |   Show History
# get-uint16maxvalue.ps1
# Maxvalue sample using PowerShell
# Thomas Lee - tfl@psp.co.uk
 
# Create test a value in range of uint16
[int] $IntegerValue = 1216
[system.int16] $uIntegerValue = 0
 
# Print it out
"Test 1"
"Integer value = $Integervalue"
 
# Now test and print results
if ($integerValue -ge [System.uint16]::MinValue -and $integerValue -le [System.uint16]::MaxValue) {
$uIntegerValue = $integerValue
"The short value is: $uIntegerValue"
}
else
{
"Unable to convert {0} to a UInt16" -f $integerValue
}
""
 
# now test one out of range
"Test 2"
if (99999999 -ge [System.uint16]::MinValue -and 99999999 -le [System.uint16]::MaxValue) {
$uIntegerValue = 99999999
"The short value is: $uIntegerValue"
}
else
{
"Unable to convert {0} to a UInt16" -f 99999999
}

#

This script produces the following output:

PSH [C:\foo]: .\get-uint16fields.ps1
Test 1
Integer value = 1216
The short value is: 1216
Test 2
Unable to convert 99999999 to a UInt16
 
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker