Convert versus parse

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

s is a string
f is a single precision float

f = float.Parse(s);// (a)
f = Convert.ToSingle(s);// (b)

Is (a) just as good as (b)?
If one should be used, not the other, please give the reason.

Patrick.
 
They are the same. In the doc it says:

The return value is the result of invoking the
Single.Parse method on value.

In other words, Convert will just call Parse anyways.

Tu-Thach
 
Hi,

One difference to keep in mind is that Convert.ToSingle(string s) will
check the in-comming value and if it is a null reference it will return
0, while float.Parse(string s) will throw an ArgumentNullException if
the value passed is a null reference.

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
Back
Top