473,386 Members | 1,693 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,386 software developers and data experts.

casting vs System.Convert

Is there any significant difference between

int variable1 = (int) variable2;

and

int variable1 = Convert.ToInt32(variable2);

It seems like the extra typing and granularity of .ToInt32 vs. .ToInt64 etc.
is a bit of overkill for most situations compared to the very simple and
straightforward cast. Is the IL the same anyway, but the compiler has to do
a little more work in the first case to figure out the conversion to use?
Nov 15 '05 #1
5 6879
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
Is there any significant difference between

int variable1 = (int) variable2;

and

int variable1 = Convert.ToInt32(variable2);


Absolutely. However, the exact difference will depend on the type of
variable2.

For instance, if variable2 has a value which is a string reference to
"23" then Convert.ToInt32 will parse the int. However, you can't *cast*
from a string to an int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Ah, yes that would make sense.

In this particular case variable2 is an enum, which according to the
documentation is stored by default as an integer. It would seem the
casting/conversion is rather cosmetic (to satisfy the compiler strong
typing).

So, what about this specific case? Any difference between the two and why?

But more generally speaking, I was really thinking only about between number
types - conversions from strings are a whole nutter ball game in my opinion.
Does your answer still depend on the two numeric types (assuming for the
sake of this discussion we're talking only about widening conversions)?

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
Is there any significant difference between

int variable1 = (int) variable2;

and

int variable1 = Convert.ToInt32(variable2);


Absolutely. However, the exact difference will depend on the type of
variable2.

For instance, if variable2 has a value which is a string reference to
"23" then Convert.ToInt32 will parse the int. However, you can't *cast*
from a string to an int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
I would use (int) MyEnum. Most likely once compiled there will be no conversion required (assuming your enum is an Int32 already),
but the Convert.ToInt32() will probably still call the ToInt32 function.

--
Michael Culley
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message news:up**************@TK2MSFTNGP12.phx.gbl...
Ah, yes that would make sense.

In this particular case variable2 is an enum, which according to the
documentation is stored by default as an integer. It would seem the
casting/conversion is rather cosmetic (to satisfy the compiler strong
typing).

So, what about this specific case? Any difference between the two and why?

But more generally speaking, I was really thinking only about between number
types - conversions from strings are a whole nutter ball game in my opinion.
Does your answer still depend on the two numeric types (assuming for the
sake of this discussion we're talking only about widening conversions)?

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
Is there any significant difference between

int variable1 = (int) variable2;

and

int variable1 = Convert.ToInt32(variable2);


Absolutely. However, the exact difference will depend on the type of
variable2.

For instance, if variable2 has a value which is a string reference to
"23" then Convert.ToInt32 will parse the int. However, you can't *cast*
from a string to an int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #4
casting also seems fast. Have not tested Convert yet. But on my machine
doing 300 million casts took about 14 seconds in a loop.

--
William Stacey, DNS MVP

"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:uV**************@TK2MSFTNGP11.phx.gbl...
Is there any significant difference between

int variable1 = (int) variable2;

and

int variable1 = Convert.ToInt32(variable2);

It seems like the extra typing and granularity of .ToInt32 vs. .ToInt64 etc. is a bit of overkill for most situations compared to the very simple and
straightforward cast. Is the IL the same anyway, but the compiler has to do a little more work in the first case to figure out the conversion to use?

Nov 15 '05 #5
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
Ah, yes that would make sense.

In this particular case variable2 is an enum, which according to the
documentation is stored by default as an integer. It would seem the
casting/conversion is rather cosmetic (to satisfy the compiler strong
typing).

So, what about this specific case? Any difference between the two and why?

But more generally speaking, I was really thinking only about between number
types - conversions from strings are a whole nutter ball game in my opinion.
Does your answer still depend on the two numeric types (assuming for the
sake of this discussion we're talking only about widening conversions)?


Between two numeric types, I'd always cast. There's still a difference
when it comes to overflow cases - unless you've got checking on, the
cast won't throw an exception, whereas the Convert call would.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

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

Similar topics

1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
1
by: fabrice | last post by:
Hello I have a little problem with casting in a Datagrid Control. I'm using Option Strict in my web application. In a Template Column, I put in an ImageButton Control with a Command...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
5
by: MikeY | last post by:
Hi Everyone, Is there a way (syntax) for converting/casting color so I can find the System.Byte of my chosen color with in my button? I have buttons colored ie "Color.Olive" and up to now I...
2
by: stunt016 | last post by:
I have a program written in C# that handles communication between two pieces of software. My problem only deals with getting a text array from one program to this C# "Bridge". I can get the text...
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
14
by: kanepart2 | last post by:
Hi guys, I am having a problem with the following code snippet:- double x = (myReader); double y = (myReader); Resulting in the follwing compilation error: Cannot implicitly convert type...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.