casting vs System.Convert

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

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?
 
Daniel Billingsley said:
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.
 
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)?
 
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 said:
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)?
 
casting also seems fast. Have not tested Convert yet. But on my machine
doing 300 million casts took about 14 seconds in a loop.
 
Daniel Billingsley said:
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.
 
Back
Top