Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Fine - 27 Aug 2007 18:12 GMT
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
Sherif Elmetainy - 27 Aug 2007 18:47 GMT
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

> 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
[quoted text clipped - 9 lines]
>
> -KF
james - 27 Aug 2007 19:00 GMT
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("Overflow_Int32"));
}

-James
Sherif Elmetainy - 27 Aug 2007 22:58 GMT
Yes, you are right :) Sorry about that.

> Sherif,
>
[quoted text clipped - 33 lines]
>
> -James
pedrito - 27 Aug 2007 18:58 GMT
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.

> 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
[quoted text clipped - 9 lines]
>
> -KF
Ken Fine - 29 Aug 2007 05:18 GMT
Thanks everyone! I appreciate the detailed examples, very succinct and
clear.

-KF

> Well, casting is done by the compiler itself whereas Convert.To.... is a
> method call. Performance-wise, the cast is generally preferred.
[quoted text clipped - 18 lines]
>>
>> -KF
Hilton - 16 Sep 2007 22:33 GMT
(int) <double> is a LOT faster than Convert.ToInt32 (<double>)

YMMV with other types of parameters.

Hilton

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.