Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I use Convert class?

For instance, in the following scenario

Convert.Int64(string somstring)

or

long.Parse(string somstring);

appear to be doing same kind of work. Which one is a better practice ?

Thanks

like image 821
Asad Avatar asked Mar 18 '26 14:03

Asad


1 Answers

If you look at the source, Convert.ToInt64(string) simply calls long.Parse, but checks that the string isn't null first.

I would recommend callings long.Parse because it makes the intent (string parsing) clearer.

I recommend using the Convert class if the type that you're converting from might change. (Or if you're converting from object)

like image 169
SLaks Avatar answered Mar 20 '26 03:03

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!