我想知道,在速度方面,以下哪种方法更可取?
//Dictionary dic<string, int>;
int getElementByKey1(string key)
{
if(dic.ContainsKey(key)) //Look-up 1
return dic[key]; //Look-up 2 in case of a "hit"
return null;
}
int getElementByKey2(string key)
{
try
{
return dic[key]; //Single look-up in case of a "hit"
}
catch
{
return null; //Exception in case of a "miss"
}
}