文字列を数値に変換するか検証


string s = "123";
int num;
if (int.TryParse(s, out num))
{
Console.WriteLine("{0} は、数字です。", s);
}
else
{
Console.WriteLine("{0} は、数字ではありません。", s);
}

s = "abc";
if (int.TryParse(s, out num))
{
Console.WriteLine("{0} は、数字です。", s);
}
else
{
Console.WriteLine("{0} は、数字ではありません。", s);
}