fastest way to change type

Y

yurps

which is the fastest or best out of these

Convert.ToInt32();
(int)x;
int.Parse(x);


Thanks,
yurps
 
H

Hans Kesting

yurps said:
which is the fastest or best out of these

Convert.ToInt32();
(int)x;
int.Parse(x);


Thanks,
yurps

It depends on what you want to convert it FROM.
(int)x doesn't work for a string-x, int.Parse
*only* works on strings.

You could try it yourself with a small program:
- Remember System.DateTime.Now
- do the conversion a lot of times (100.000)
- get a fresh DateTime.Now
- subtract the times.
Repeat a couple of times to get an average value.

Repeat again for an other conversion.
 
K

Karl Seguin

I refuse to answer this question for two reasons

First, not all methods do the same thing

string str = "1";
int i = Convert.ToInt32(str ); //this will work
int i = (int) str //this won't work

so you aren't comparing apples to apples

secondly, any difference would be so absolutely minor and any major
differences would be so specific to a given situation, no one could
accurately answer save the person asking...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
K

Karl Seguin

int.Parse only accepts strings

Convert.ToInt32() accepts more type

when passing in a string, Convert.ToInt32() simply calls int.Parse...it's
actually a bit slower, because it does a null check...but that also makes it
100% better to use if you ask me...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top