How to test string for an Integer?

This is quite common task: to check whether some string represents an integer number or not. Using Parse method and catching an exception is quite costly from a performance point.
So use Int32.TryParse() method.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
________________________________________
public static bool TryParse(
string s,
out int result
)
Parameters
s : Type: System.String - A string containing a number to convert.
result : Type: System.Int32 - When this method returns, contains the 32-bit signed integer value 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, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
Return Value
Type: System.Boolean - true if s was converted successfully; otherwise, false.
________________________________________
The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.
This overload of the TryParse method interprets all digits in the s parameter as decimal digits. To parse the string representation of a hexadecimal number, call the Int32.TryParse(String, NumberStyles, IFormatProvider, Int32) overload.
Examples, how to check whether a string contains an integer or not.
________________________________________
private void Test()
{
CheckIsInteger("281");
CheckIsInteger("-547");
CheckIsInteger("sdsdsd");
CheckIsInteger("0ewew");
CheckIsInteger("589.0");
CheckIsInteger("02FA");
CheckIsInteger("+297");
}
private void CheckIsInteger(string value)
{
int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
Console.WriteLine("The " + value + " is integer.");
}
else
{
Console.WriteLine("The " + value + " is not integer.");
}
}
The result is:
The 281 is integer.
The -547 is integer.
The sdsdsd is not integer.
The 0ewew is not integer.
The 589.0 is not integer.
The 02FA is not integer.
The +297 is integer.
The Int32.TryParse() method supported in such versions of .NET Framework:
4, 3.5, 3.0, 2.0