By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

basic q: What are the differences between casting and Convert.ToXXX methods

P: n/a
This is a basic question. What is the difference between casting and using
the Convert.ToXXX methods, from the standpoint of the compiler, in terms of
performance, and in other ways? e.g.

this.ContentID = (int)ci.Conid;
vs.
this.ContentID = Convert.ToInt32(ci.Conid);

I tend to use the latter form because it seems more descriptive to me, but
it would be good to know what's best practice. I'm guessing those methods
are more expensive computationally.

-KF

Aug 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a

The difference it terms of functionality is shown in the following examples:

object a = "45"; // a is of type String
int b = (int)a; // InvalidCastException is thrown
int c= Convert.ToInt32(a); // c will contain 45

string d = "45";
int e = (int)d; // compiler error
int f = Convert.ToInt32(d); // f will contain 45

string g = "test";
int h = Convert.ToInt32(g); // FormatException is thrown because
Convert.ToInt32 will attempt to call Int32.Parse

long i = long.MaxValue;
unchecked
{
int j = (int)i; // OverflowException is thrown;
int k = Convert.ToInt32(i); // OverflowException is thrown
}
checked
{
int l = (int)d; // f will contain -1
int m = Convert.ToInt32(i); // OverflowException is thrown
(checked/unchecked doesn't affect Convet.ToXXX methods)
}
Depending on which overload of Convert.ToXXX you are calling, the
performance can be a bit slower than type casting (in case of the overload
that takes an object as a parameter) and functionality can be different, but
in many cases performance should be the same assuming that the JIT compiler
will inline the Convert.ToXXX calls.

Regards,
Sherif
"Ken Fine" <ke*****@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
This is a basic question. What is the difference between casting and using
the Convert.ToXXX methods, from the standpoint of the compiler, in terms
of performance, and in other ways? e.g.

this.ContentID = (int)ci.Conid;
vs.
this.ContentID = Convert.ToInt32(ci.Conid);

I tend to use the latter form because it seems more descriptive to me, but
it would be good to know what's best practice. I'm guessing those methods
are more expensive computationally.

-KF

Aug 27 '07 #2

P: n/a
Well, casting is done by the compiler itself whereas Convert.To.... is a
method call. Performance-wise, the cast is generally preferred.

Convert.To, I find, is generally more handy for converting strings to
numbers. I rarely use it for anything else. I don't know that I've ever used
it to cast from one number to another. I don't really see that there's any
advantage and the casting looks cleaner and easier to read, in my opinion.

"Ken Fine" <ke*****@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
This is a basic question. What is the difference between casting and using
the Convert.ToXXX methods, from the standpoint of the compiler, in terms
of performance, and in other ways? e.g.

this.ContentID = (int)ci.Conid;
vs.
this.ContentID = Convert.ToInt32(ci.Conid);

I tend to use the latter form because it seems more descriptive to me, but
it would be good to know what's best practice. I'm guessing those methods
are more expensive computationally.

-KF

Aug 27 '07 #3

P: n/a
Sherif,

I think checked and unchecked are backwards :)

System.Convert methods also perform rounding. For instance here is
some disassembly

public static int ToInt32(double value)
{
if (value >= 0)
{
if (value < 2147483647.5)
{
int num = (int) value;
double num2 = value - num;
if ((num2 0.5) || ((num2 == 0.5) && ((num & 1) != 0)))
{
num++;
}
return num;
}
}
else if (value >= -2147483648.5)
{
int num3 = (int) value;
double num4 = value - num3;
if ((num4 < -0.5) || ((num4 == -0.5) && ((num3 & 1) != 0)))
{
num3--;
}
return num3;
}
throw new
OverflowException(Environment.GetResourceString("O verflow_Int32"));
}

-James



Aug 27 '07 #4

P: n/a
Yes, you are right :) Sorry about that.

"james" <ja********@gmail.comwrote in message
news:11*********************@r29g2000hsg.googlegro ups.com...
Sherif,

I think checked and unchecked are backwards :)

System.Convert methods also perform rounding. For instance here is
some disassembly

public static int ToInt32(double value)
{
if (value >= 0)
{
if (value < 2147483647.5)
{
int num = (int) value;
double num2 = value - num;
if ((num2 0.5) || ((num2 == 0.5) && ((num & 1) != 0)))
{
num++;
}
return num;
}
}
else if (value >= -2147483648.5)
{
int num3 = (int) value;
double num4 = value - num3;
if ((num4 < -0.5) || ((num4 == -0.5) && ((num3 & 1) != 0)))
{
num3--;
}
return num3;
}
throw new
OverflowException(Environment.GetResourceString("O verflow_Int32"));
}

-James



Aug 27 '07 #5

P: n/a
Thanks everyone! I appreciate the detailed examples, very succinct and
clear.

-KF

"pedrito" <pixbypedrito at yahoo.comwrote in message
news:Lt******************************@giganews.com ...
Well, casting is done by the compiler itself whereas Convert.To.... is a
method call. Performance-wise, the cast is generally preferred.

Convert.To, I find, is generally more handy for converting strings to
numbers. I rarely use it for anything else. I don't know that I've ever
used it to cast from one number to another. I don't really see that
there's any advantage and the casting looks cleaner and easier to read, in
my opinion.

"Ken Fine" <ke*****@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>This is a basic question. What is the difference between casting and
using the Convert.ToXXX methods, from the standpoint of the compiler, in
terms of performance, and in other ways? e.g.

this.ContentID = (int)ci.Conid;
vs.
this.ContentID = Convert.ToInt32(ci.Conid);

I tend to use the latter form because it seems more descriptive to me,
but it would be good to know what's best practice. I'm guessing those
methods are more expensive computationally.

-KF

Aug 29 '07 #6

P: n/a
(int) <doubleis a LOT faster than Convert.ToInt32 (<double>)

YMMV with other types of parameters.

Hilton
Sep 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.