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

ToInt

tries to parse a string to an int, returns zero if it is unable to parse

Source

public static int ToInt(this string current)
{
	int convertedValue;
	
	int.TryParse(current, out convertedValue);

	return convertedValue;
}

Example

var someString = "5";
var someInt = someString.ToInt();

Author: Sean Biefeld

Submitted on: 10 nov. 2010

Language: C#

Type: System.String

Views: 5135