473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Discussion

string mynumberstring = "123";
int mynumber;
mynumber = int.Parse(mynumberstring);
mynumber = Convert.ToInt32(mynumberstring);

Both seem to have the same effect. But whats the
difference and which is 'best'?

Thanks, Paul.
Nov 15 '05 #1
2 1291
Paul,

Int32.Parse has the most flexibility, with the capability of passing in
a string, a value from the NumberStyles enumeration, and a format provider.
ToInt32 will only accept the string and a format provider. The Convert
class has the advantage of being able to take any type and trying to convert
to an Int32 (not always successful though).

So, you should pick the one that suits your needs best and then use
that. I don't know if there is a perf difference, but I am sure someone can
run some tests.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Paul" <qw*@qwe.com> wrote in message
news:0b****************************@phx.gbl...
string mynumberstring = "123";
int mynumber;
mynumber = int.Parse(mynumberstring);
mynumber = Convert.ToInt32(mynumberstring);

Both seem to have the same effect. But whats the
difference and which is 'best'?

Thanks, Paul.

Nov 15 '05 #2
Paul,

I ran my own tests (just a loop), and I ran each one for ten million
times. I found there to be a slight advantage in the call to Int32.Parse,
by about .2 - .4 seconds. Unless you are doing an ungodly amount of
conversions, I wouldn't worry about it.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Paul" <qw*@qwe.com> wrote in message
news:0b****************************@phx.gbl...
string mynumberstring = "123";
int mynumber;
mynumber = int.Parse(mynumberstring);
mynumber = Convert.ToInt32(mynumberstring);

Both seem to have the same effect. But whats the
difference and which is 'best'?

Thanks, Paul.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: stenknz | last post by:
Does any one know where there is a good introduction to Discussion Forum scripting using PHP and MySQL. (Apache WS) I just need the nuts and bolts to get an overview of whats required. Regards,...
21
by: Rune Steffensen | last post by:
I've been lurking c.l.p for a while now, and find the amount of messages a bit annoying. To solve this, I suggest the creation of the new group "comp.lang.pyhon.discussions". So when people post...
1
by: Fuzzyman | last post by:
There is a `web design` group over on google-groups. http://groups-beta.google.com/group/wd It's brief is for ``Discussion of web design (html, php, flash, wysiwig, cgi, perl, python, css,...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
0
by: Harry Smith | last post by:
This was posted in news.groups, but it was not posted to the list. REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.postgresql.admin unmoderated group...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
1
by: Carl J. Van Arsdall | last post by:
Hey everyone, I know I've posted several questions regarding python and python's parallel capabilities so bear with me as I've never attempted to incite discussion. However, today I'm...
3
by: Duncan Smith | last post by:
Hello, Since moving to numpy I've had a few problems with my existing code. It basically revolves around the numpy scalar types. e.g. ------------------------------------------------ array(,...
10
by: jacob navia | last post by:
There is a very interesting thread in comp.lang.c++ about garbage collection. It is very instructive to compare the level of the discussion there with the discussion we just had here in...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.