Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Parse versus Convert

Tags:

.net

parsing

In .Net you can read a string value into another data type using either <datatype>.parse or Convert.To<DataType>.

I'm not familiar with the fundamentals of parse versus convert so I am always at a loss when asked which one is better/faster/more appropriate.

So - which way is best in what type of circumstances?

like image 997
Rob Allen Avatar asked Aug 20 '08 18:08

Rob Allen


People also ask

What is the difference between Parse and convert in C#?

Convert. ToInt32 allows null value, it doesn't throw any errors Int. parse does not allow null value, and it throws an ArgumentNullException error.

Why do we Parse in C#?

The Parse method returns the converted number; the TryParse method returns a boolean value that indicates whether the conversion succeeded, and returns the converted number in an out parameter. If the string isn't in a valid format, Parse throws an exception, but TryParse returns false .

Why do we use convert ToInt32 in C#?

ToInt32(String, IFormatProvider) Method. This method is used to converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information.

Which statement accurately describes the difference between Int32 TryParse () and convert ToInt32 ()?

Parse() and Int32. TryParse() can only convert strings. Convert. ToInt32() can take any class that implements IConvertible .


3 Answers

The Convert.ToXXX() methods are for objects that might be of the correct or similar type, while .Parse() and .TryParse() are specifically for strings:

//o is actually a boxed int
object o = 12345;

//unboxes it
int castVal = (int) 12345;

//o is a boxed enum
object o = MyEnum.ValueA;

//this will get the underlying int of ValueA
int convVal = Convert.ToInt32( o );

//now we have a string
string s = "12345";

//this will throw an exception if s can't be parsed
int parseVal = int.Parse( s );

//alternatively:
int tryVal;
if( int.TryParse( s, out tryVal ) ) {
    //do something with tryVal 
}

If you compile with optimisation flags TryParse is very quick - it's the best way to get a number from a string. However if you have an object that might be an int or might be a string Convert.ToInt32 is quicker.

like image 173
Keith Avatar answered Oct 17 '22 17:10

Keith


Here's an answer for you:

http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=77428

Though I think in modern versions of .NET, the best thing to do is use TryParse in any case, if there's any doubt that the conversion will work.

like image 5
TheSmurf Avatar answered Oct 17 '22 18:10

TheSmurf


I'm a big fan of TryParse, since it saves you a lot of headache of error catching when there's a chance the value you're going to parse is not of the appropriate type.

My order is usually:

  • Parse (if I can be sure the value will be the right type, and I do try to ensure this)
  • TryParse (if I can't be sure, which happens whenever user input is involved, or input from a system you cannot control)
  • Convert (which I think I have not used since I started using Parse and TryParse, but I could be wrong)
like image 3
saalon Avatar answered Oct 17 '22 19:10

saalon