C# UInt16 TryParse(String, UInt16)

Description

UInt16 TryParse(String, UInt16) tries to convert the string representation of a number to its 16-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

UInt16.TryParse(String, UInt16) has the following syntax.


[CLSCompliantAttribute(false)]// ww  w  . j a  va2  s .c  o m
public static bool TryParse(
  string s,
  out ushort result
)

Parameters

UInt16.TryParse(String, UInt16) has the following parameters.

  • s - A string that represents the number to convert.
  • result - When this method returns, contains the 16-bit unsigned integer value that is equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in the correct format. , or represents a number less than UInt16.MinValue or greater than UInt16.MaxValue. This parameter is passed uninitialized.

Returns

UInt16.TryParse(String, UInt16) method returns true if s was converted successfully; otherwise, false.

Example

The following example calls the TryParse(String, UInt16) method once for each element in a string array.


/*from   w w  w. j  a v  a  2  s  .c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string[] numericStrings = new string[] { "1234.8", "+1234.7", "12345.",  
                                "   12345123  ", "(0)", "-0", "-1", 
                                "+12345678", "18-", "123456", "12,345", 
                                "  1234567 ", "00723400" };
    uint number;
    foreach (string numericString in numericStrings)
    {
       if (UInt32.TryParse(numericString, out number)) 
          Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
       else
          Console.WriteLine("Cannot convert '{0}' to a UInt32.", numericString);
    }

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version