C# UInt32 TryParse(String, UInt32)
Description
UInt32 TryParse(String, UInt32)
tries to convert the
string representation of a number to its 32-bit unsigned integer equivalent.
A return value indicates whether the conversion succeeded or failed.
Syntax
UInt32.TryParse(String, UInt32)
has the following syntax.
[CLSCompliantAttribute(false)]//from w w w .ja v a2s . c o m
public static bool TryParse(
string s,
out uint result
)
Parameters
UInt32.TryParse(String, UInt32)
has the following parameters.
s
- A string that represents the number to convert.result
- When this method returns, contains the 32-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 of the correct format, or represents a number that is less than UInt32.MinValue or greater than UInt32.MaxValue. This parameter is passed uninitialized.
Returns
UInt32.TryParse(String, UInt32)
method returns true if s was converted successfully; otherwise, false.
Example
The following example calls the TryParse(String, UInt32) method once for each element in a string array.
//from ww w.j ava 2 s .com
using System;
public class MainClass{
public static void Main(String[] argv){
string[] numericStrings = { "1234.8", "+1234.7", "12345.",
" 12345667 ", "(0)", "-0", "-1",
"+1234567", "18-", "123456", "12,345",
" 1234567 ", "12345000" };
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.