Differences between these cast operations / statements?


G

Guest

Hi,
i read this
http://msdn.microsoft.com/chats/transcripts/vstudio/05_0811_dn_csharp.aspx :

Cyrusn_MS (Expert):
Q: please explain the difference
1: Convert.ToInt32(o)
2: (int)o
3: o as int32
A: Rk: It all depends on what o's type is. Convert.ToInt32 will convert a
lot o standard framework types into an int32. For example, it will allow you
to convert 'true' into '1'. (int)o performs an explicit cast from the o
object to int, and will fail if that type doesn't have such a conversion
(like bool). o as Int32 is not allowed as you cannot have a value type on the
right side of an 'as'. THat's because 'as' returns null if it couldn't
perform the conversion and there is no null Int32 value.

maybe someone can explain this more in detail?

i have an additional point 4: int.Parse() whats about that?
thanks, toebens
 
Ad

Advertisements

T

Truong Hong Thi

int.Parse accepts a string, not object. It tries to parse the string
into the int32 value, and will fail with FormatException if the string
is not a valid representation of an int.
The overloaded method Convert.ToInt32(string), call this method
internally.
 
M

^MisterJingo^

Regarding (int)o, if it fails an exception will be thrown, whereas o as
int32 will return null if it 'fails' but will not thrown an exception.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

^MisterJingo^
As the original poster correctly stated *as* operator cannot be used in this
case (the case of converting to value types) because one of the possible
return values is *null*. Value types cannot be initialized with *null* Using
*as* in this case is compilation error.

toebens, which part of the conversion is not clear to you? As I can see you
got it correct.
 
G

Guest

toebens, which part of the conversion is not clear to you? As I can see you

so if i got it all then:
1: Convert.ToInt32 can be used for multiple datatypes
2: (int) only with an object - this object must be a "boxed" integer
3: o as int - cant be used because int cant be null - o as string would work
4: int.Parse() only works with a string as parameter

is that all right?

so i have to convert a ViewState["mySiteID"] with Convert.ToInt32 because it
ViewState returns an object!?? (i'm using this in asp.net)
 
B

Bruce Wood

2: (int) only with an object - this object must be a "boxed" integer

No, you can also say:

long aLong = 15;
int anInt = (int)aLong;

or

int x = 15;
int y = (int)x;

The latter is useless, but I believe it's allowed. The (int) cast
operator is used for two things: 1) To indicate to the compiler that
even though it doesn't know for sure that an object you've given it is
really an int, _you_ know, and it should generate code to unbox the
value into an int... if it's a boxed integer, as you pointed out, or 2)
To indicate an explicit coercion from one integer type to another.
so i have to convert a ViewState["mySiteID"] with Convert.ToInt32 because it
ViewState returns an object!?? (i'm using this in asp.net)

Well, that all depends. the ViewState indexer is _declared_ as
returning an object, but that means that it can return any object of
any type. So, what type is that object, really? That is, don't forget
the distinction between compile time and run time. At compile time, all
the compiler knows is that the ViewState indexer returns some kind of
object. At run time, however, that object will be of a specific type, a
type not predictable at compile time, but a specific type nonetheless.
So, what type is it?

If it's a boxed integer, or a boxed long, or... you can say

int id = (int)ViewState["MySiteID"];

If it's a string or something else, you'll need to say:

int id = Convert.ToInt32(ViewState["MySiteID"]);

If you have no idea what it will be, then you should use
Convert.ToInt32, as this will accept even an Int32 and "convert" it to
Int32, so it works in all valid cases.

The bottom line is that even though the compiler doesn't know what the
return type is, doesn't mean that _you_ don't know and can't instruct
the compiler appropriately.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top