My application reads an Excel file using VSTO and adds the read data to a StringDictionary
. It adds only data that are numbers with a few digits (1000 1000,2 1000,34 - comma is a delimiter in Russian standards).
What is better to check if the current string is an appropriate number?
object data, string key; // data had read
try
{
Convert.ToDouble(regionData, CultureInfo.CurrentCulture);
dic.Add(key, regionData.ToString());
}
catch (InvalidCastException)
{
// is not a number
}
or
double d;
string str = data.ToString();
if (Double.TryParse(str, out d)) // if done, then is a number
{
dic.Add(key, str);
}
I have to use StringDictionary
instead of Dictionary<string, double>
because of the following parsing algorithm issues.
My questions: Which way is faster? Which is safer?
And is it better to call Convert.ToDouble(object)
or Convert.ToDouble(string)
?
Lots of hate for the Convert class here... Just to balance a little bit, there is one advantage for Convert - if you are handed an object,
can just return the value easily if o is already a Double (or an int or anything readily castable).
Using Double.Parse or Double.TryParse is great if you already have it in a string, but
has to go make the string to be parsed first and depending on your input that could be more expensive.
This is an interesting old question. I'm adding an answer because nobody noticed a couple of things with the original question.
Which is faster: Convert.ToDouble or Double.TryParse? Which is safer: Convert.ToDouble or Double.TryParse?
I'm going to answer both these questions (I'll update the answer later), in detail, but first:
For safety, the thing every programmer missed in this question is the line (emphasis mine):
Followed by this code example:
What's interesting here is that if the spreadsheets are in Russian number format but Excel has not correctly typed the cell fields, what is the correct interpretation of the values coming in from Excel?
Here is another interesting thing about the two examples, regarding speed:
This is likely going to generate MSIL that looks like this:
In this sense, we can probably compare the total number of MSIL instructions carried out by each program - more on that later as I update this post.
I believe code should be Correct, Clear, and Fast... In that order!
The .NET Framework design guidelines recommend using the Try methods. Avoiding exceptions is usually a good idea.
Convert.ToDouble(object)
will do((IConvertible) object).ToDouble(null);
Which will call
Convert.ToDouble(string, null)
So it's faster to call the string version.
However, the string version just does this:
So it's faster to do the
double.Parse
directly.If you aren't going to be handling the exception go with TryParse. TryParse is faster because it doesn't have to deal with the whole exception stack trace.
Double.TryParse IMO.
It is easier for you to handle, You'll know exactly where the error occurred.
Then you can deal with it how you see fit if it returns false (i.e could not convert).