Convert or Parse
-
I was told that the most common method to convert a value is to use the
Parse
method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example...Convert.ToInt32(value);
orint.Parse(value.ToString());
Thanks guys! -
I was told that the most common method to convert a value is to use the
Parse
method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example...Convert.ToInt32(value);
orint.Parse(value.ToString());
Thanks guys!Hi, it depends on the Type of your value: if value is a string, then value.ToString() is redundant, and both Convert and int.Parse would behave identically (in fact one is calling the other). if value is some kind of integer to begin with, Convert.ToInt32() is much faster, since no conversion from integer to string, and no conversion from string to int are involved. if value is something else (a DateTime, a user-defined type, ...) Convert will not accept it, whereas the value.ToString() result may or may not look like an integer, and be acceptable to int.Parse :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:02 AM
-
I was told that the most common method to convert a value is to use the
Parse
method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example...Convert.ToInt32(value);
orint.Parse(value.ToString());
Thanks guys!Luc summed it up well, but I just wanted to add one other non-obvious difference. With value = null; Convert.ToInt32(value) returns 0 int.Parse(value.ToString()) throws NullReferenceException. int.Parse(value) throws ArgumentNullException. (if value is String)
-
Luc summed it up well, but I just wanted to add one other non-obvious difference. With value = null; Convert.ToInt32(value) returns 0 int.Parse(value.ToString()) throws NullReferenceException. int.Parse(value) throws ArgumentNullException. (if value is String)
hi, look at the int.TryParse(object,out bool) functionality. The boolean value stands up for true (parseable) and false(not parseable) about trying to parse the value. There will be no exception raised. if you want to use int.Parse instead of TryParse you need to catch the exception with try catch.. bless :)
-
I was told that the most common method to convert a value is to use the
Parse
method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example...Convert.ToInt32(value);
orint.Parse(value.ToString());
Thanks guys!In my opinion the only worthwhile member of Convert is
System.Convert.ChangeType
, will that do what you want? Otherwise, maybe a simple cast is what you want? Or, if it's a type of your own creation, perhaps an implicit conversion operator? You would have to give us more information. -
In my opinion the only worthwhile member of Convert is
System.Convert.ChangeType
, will that do what you want? Otherwise, maybe a simple cast is what you want? Or, if it's a type of your own creation, perhaps an implicit conversion operator? You would have to give us more information.Hijack If you have 2 classes that are identical (set of credentials) can you use
System.Convert.ChangeType
to convert one to the other?Never underestimate the power of human stupidity RAH
-
I was told that the most common method to convert a value is to use the
Parse
method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example...Convert.ToInt32(value);
orint.Parse(value.ToString());
Thanks guys!The most common method to convert a non-string value to an int is implicit or explicit casting. Example:
// implicit casting:
byte b = 42;
int answer = b;// explicit casting:
double d = 42.0;
int answer = (int)d;Despite everything, the person most likely to be fooling you next is yourself.