string applesInBasket = "12";
string baskets = "4";
int total = Convert.ToInt32(applesInBasket) * Convert.ToInt32(baskets);
string msg = $"There are total of {total} apples";
Console.WriteLine(msg);
在示例中,我们将两个最初为字符串的整数值相乘。
int total = Convert.ToInt32(applesInBasket) * Convert.ToInt32(baskets);
string val = "23423453263456345";
try
{
int num = Int32.Parse(val);
Console.WriteLine($"Converted '{val}' to {num}.");
}
catch (FormatException)
{
Console.WriteLine($"Unable to convert '{val}'.");
}
catch (OverflowException)
{
Console.WriteLine($"'{val}' is out of range of the Int32 type.");
}