UInt16.ToString Method in C# with Examples

The UInt16.ToString() method in C# is used to convert the numeric value of the current UInt16 instance to its equivalent string representation. The UInt16 data type represents a 16-bit unsigned integer with a range from 0 to 65,535.

This method has multiple overloads that allow you to format the string representation using different format specifiers, culture-specific formatting, and format providers.

Syntax

Following is the syntax for the basic ToString() method −

public override string ToString()

Following is the syntax for ToString() with format specifier −

public string ToString(string format)

Following is the syntax for ToString() with format provider −

public string ToString(IFormatProvider provider)

Parameters

  • format − A standard or custom numeric format string (optional)

  • provider − An object that supplies culture-specific formatting information (optional)

Return Value

Returns a string representation of the UInt16 value in the specified format.

Using Basic ToString() Method

The basic ToString() method converts a UInt16 value to its string representation without any special formatting −

using System;

public class Demo {
   public static void Main() {
      ushort val1 = 100;
      ushort val2 = 80;
      
      Console.WriteLine("Value1 (String representation) = " + val1.ToString());
      Console.WriteLine("Value2 (String representation) = " + val2.ToString());
      
      bool res = val1.Equals(val2);
      Console.WriteLine("Return value (comparison) = " + res);
      
      if (res)
         Console.WriteLine("val1 = val2");
      else
         Console.WriteLine("val1 != val2");
   }
}

The output of the above code is −

Value1 (String representation) = 100
Value2 (String representation) = 80
Return value (comparison) = False
val1 != val2

Using ToString() with Min and Max Values

This example demonstrates ToString() with the minimum and maximum values of UInt16

using System;

public class Demo {
   public static void Main() {
      ushort val1 = UInt16.MinValue;
      ushort val2 = UInt16.MaxValue;
      
      Console.WriteLine("Value1 (String representation) = " + val1.ToString());
      Console.WriteLine("Value2 (String representation) = " + val2.ToString());
      Console.WriteLine("UInt16 Min Value: " + UInt16.MinValue);
      Console.WriteLine("UInt16 Max Value: " + UInt16.MaxValue);
      
      bool res = val1.Equals(val2);
      Console.WriteLine("Return value (comparison) = " + res);
      
      if (res)
         Console.WriteLine("val1 = val2");
      else
         Console.WriteLine("val1 != val2");
   }
}

The output of the above code is −

Value1 (String representation) = 0
Value2 (String representation) = 65535
UInt16 Min Value: 0
UInt16 Max Value: 65535
Return value (comparison) = False
val1 != val2

Using ToString() with Format Specifiers

You can use format specifiers to control how the UInt16 value is converted to string −

using System;

public class Demo {
   public static void Main() {
      ushort value = 255;
      
      Console.WriteLine("Decimal format: " + value.ToString("D"));
      Console.WriteLine("Hexadecimal format: " + value.ToString("X"));
      Console.WriteLine("Hexadecimal lowercase: " + value.ToString("x"));
      Console.WriteLine("Currency format: " + value.ToString("C"));
      Console.WriteLine("Fixed-point format: " + value.ToString("F2"));
      Console.WriteLine("Number format: " + value.ToString("N"));
   }
}

The output of the above code is −

Decimal format: 255
Hexadecimal format: FF
Hexadecimal lowercase: ff
Currency format: $255.00
Fixed-point format: 255.00
Number format: 255.00

Conclusion

The UInt16.ToString() method provides flexible string conversion for 16-bit unsigned integers, supporting various format specifiers and culture-specific formatting. It's essential for displaying UInt16 values in user interfaces and converting numeric data to string representations for output or storage purposes.

Updated on: 2026-03-17T07:04:35+05:30

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements