C# Convert ToUInt16() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.ToUInt16 returns a ushort—unsigned 16-bit integer from 0 to 65,535.

Negatives and values above 65535 throw OverflowException. Bad text throws FormatException.

This is a static member of System.Convert. You call it with the type name, such as Convert.ToUInt16(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Convert text or numbers into a ushort.

C#
ushort a = Convert.ToUInt16("1000");
ushort b = Convert.ToUInt16(50);
ushort c = Convert.ToUInt16("FFFF", 16);
CallWhat it does
Convert.ToUInt16("1000")Decimal string → ushort.
Convert.ToUInt16("FFFF", 16)Hex → 65535.

null → 0. Prefer ushort.TryParse for soft failure.

Library declarations
C#
public static ushort ToUInt16(string? value);
public static ushort ToUInt16(string? value, int fromBase);
public static ushort ToUInt16(int value);

Parameters

ParameterDescription
valueRequired. Text or number to convert.
fromBaseOptional. 2, 8, 10, or 16.

Return Value

Return ValueDescription
ushortUnsigned 16-bit result.

C# Examples

Example 1 — Parse an Unsigned Short

Convert a decimal string to UInt16.

C#
using System;

class Program {
    static void Main() {
        ushort n = Convert.ToUInt16("500");
        Console.WriteLine(n);
        Console.WriteLine(n + 20);
    }
}

How It Works

  1. Unsigned short. Result type is ushort.
  2. 500 fits. Inside 0…65535.
  3. Add twenty. Ordinary integer math.
  4. Null → 0. Convert remains null-friendly.

Example 2 — FFFF Hex Edge

Parse the maximum ushort via hex.

C#
using System;

class Program {
    static void Main() {
        ushort n = Convert.ToUInt16("FFFF", 16);
        Console.WriteLine(n);
        Console.WriteLine(Convert.ToUInt16("10", 2));
    }
}

How It Works

  1. FFFF. Hex max equals 65535.
  2. Binary 10. Base 2 yields 2.
  3. Edge success. Exactly max does not throw.
  4. 65536 fails. One more would overflow.

Example 3 — Reject Negatives

Show that negative ints overflow for ushort.

C#
using System;

class Program {
    static void Main() {
        try {
            Console.WriteLine(Convert.ToUInt16(-1));
        } catch (OverflowException) {
            Console.WriteLine("unsigned only");
        }
        Console.WriteLine(Convert.ToUInt16(0));
    }
}

How It Works

  1. −1 invalid. Unsigned types reject negatives.
  2. OverflowException. Catch and report.
  3. Zero ok. Minimum edge succeeds.
  4. Pick signed. Use ToInt16 when negatives matter.

Frequently Asked Questions

0 to 65535.
No—OverflowException for negatives.
Yes for typical ToUInt16 overloads.
Yes: Convert.ToUInt16("FF", 16).
Parse throws on null; Convert returns 0.

Next: ToUInt32()

Convert a value to uint (0…4294967295) with System.Convert.

ToUInt32() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful