471,854 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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
6 4001

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
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
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
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
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
(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.

Similar topics

1 post views Thread by thomson | last post: by
7 posts views Thread by Jim Bancroft | last post: by
61 posts views Thread by Ken Allen | last post: by
8 posts views Thread by John A Grandy | last post: by
23 posts views Thread by René Nordby | last post: by
5 posts views Thread by Charlie Brown | last post: by
9 posts views Thread by pauldepstein | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

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.