Tip4 TryParse比Parse好
public static double Parse(string s)
public static bool TryParse(sting s, out double result)
Parse转换失败会引发异常,通过捕获异常来设置初始值
试验:
static void Main(string[] args)
{
double re;
long ticks;
Stopwatch sw = Stopwatch.StartNew();
for (int i = 1; i < 1000; i++)
{
try
{
re = double.Parse("123");
}
catch
{
re = 0;
}
}
sw.Stop();
ticks = sw.ElapsedTicks;
Console.WriteLine("double.Parse() 成功,{0} ticks", ticks);
sw = Stopwatch.StartNew();
for (int i = 1; i < 1000; i++)
{
if (double.TryParse("123", out re) == false)
{
re = 0;
}
}
sw.Stop();
ticks = sw.ElapsedTicks;
Console.WriteLine("double.TryParse() 成功,{0} ticks", ticks);
sw = Stopwatch.StartNew();
for (int i = 1; i < 1000; i++)
{
try
{
re = double.Parse("aaa");
}
catch
{
re = 0;
}
}
sw.Stop();
ticks = sw.ElapsedTicks;
Console.WriteLine("double.Parse() 失败,{0} ticks", ticks);
sw = Stopwatch.StartNew();
for (int i = 1; i < 1000; i++)
{
if (double.TryParse("aaa", out re) == false)
{
re = 0;
}
}
sw.Stop();
ticks = sw.ElapsedTicks;
Console.WriteLine("double.TryParse() 失败,{0} ticks", ticks);
}
试验效果: