Give me an Int

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

int.Parse() OR Convert.ToInt32() OR (int)

Hi,

Is there any sort of convention as to when its appropriate to use
int.Parse(), Convert.ToInt32() or casting to int?

At the moment I am using them interchangeably. If one doesnt work, i try
another one.

I know that int.Parse is for creating an int from a string, but for
Convert.ToInt32() would do the same thing.

What are real life instances when one should be preferred over the other.?


thank you..
 
=?Utf-8?B?Q29kZVJhem9y?= said:
Is there any sort of convention as to when its appropriate to use
int.Parse(), Convert.ToInt32() or casting to int?

If from a string, its better to Parse (ie parse or Convert)
I know that int.Parse is for creating an int from a string, but for
Convert.ToInt32() would do the same thing.

Convert calls Parse, tehy are nearly identical.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
Hi,

Convert and Int.Parse are similar, with one important exception, if you are
converting a String and this string is null it will give you an exception,
wheather Convert will give you 0

a cast is a completely different thing though. It will work only if the
instance is a boxed int, or a boxed/unboxed value type that can be
converted implicitely to int .

cheers,
 
thanks Ignacio. I didnt know the important difference between int.parse and
Convert. cheers, that will be useful in the future.

The final thing you said about casting and boxing Ints, lost me a bit. Could
you demonstrate what you mean.

thank you very much
 
=?Utf-8?B?Q29kZVJhem9y?= said:
The final thing you said about casting and boxing Ints, lost me a bit.
Could you demonstrate what you mean.

Boxing is a way of represenignt value types as objects. If you have a string, and you want it converted
to an int, use parse or convert.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
Think of it this way: you can cast to an int only if it's already a
numeric type, or an object representing a numeric type (that's what
boxed means). So, for example,

double d = 15.23;
int i = (int)d;

is legal because d is numeric and can be converted to an int. The cast
is required because information will be lost (the 0.23).

int a = 15;
object o = a;
int b = (int)o;

is legal because o is a reference to a _boxed_ int: an int that has
been converted to an object so that it can be used like an object.
Since the object that o refers to is already an int, you can cast it to
an int with no problems.

I always prefer to cast if I can because it's cleaner and easier to
read. yes, I could say:

double d = 15.23;
int i = Convert.ToInt32(d);

but it's longwinded and doesn't buy me anything. I reserve Convert and
Parse for strings, and use casting for all numeric conversions.
 

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

Back
Top