ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

TryParse

This method takes away the need for writing two lines for TryParse and gives users an option of returning a default number.

Source

public static int TryParse(this string input, int defaultValue)
{
    int value;
    if (Int32.TryParse(input, out value))
    {
        return value;
    }
    return defaultValue;
}

Example

"2".TryParse(-1)=2;
"Xe".TryParse(-1)=-1;

Author: Avik

Submitted on: 7 jun. 2012

Language: C#

Type: System.Int32

Views: 8650