在有很多在我的代碼的地方,我有這樣的:避免的try-catch通過的TryParse,而不是解析
try
{
price = double.Parse(sPrice.Replace(",", "."), CultureInfo.InvariantCulture);
}
catch
{
price = 0;
}
我讀的地方,如果異常在try塊拋出,它需要大量的時間被抓住了。
所以,我想使用的TryParse,而不是解析,就像這樣:
if (!double.TryParse(sPrice, out price))
{
price = 0;
}
這是一個好的做法呢?它會花費更少的時間嗎?
您*可以*自己對其進行基準測試。至於好的做法,好吧。這完全是主觀的,但我更喜歡tryparse方法,我自己。 =) – 2013-03-05 09:27:51
是的,它將確實節省執行時間 – Dreamwalker 2013-03-05 09:28:04
可能重複的[解析訴TryParse](http://stackoverflow.com/questions/467613/parse-v-tryparse) – Habib 2013-03-05 09:29:08