By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Differences between these cast operations / statements?

P: n/a
Hi,
i read this
http://msdn.microsoft.com/chats/tran...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
Dec 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

Dec 14 '05 #2

P: n/a
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.

Dec 14 '05 #3

P: n/a
^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.
--

Stoitcho Goutsev (100) [C# MVP]
"^MisterJingo^" <mi*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.

Dec 14 '05 #4

P: n/a
> 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)

Dec 14 '05 #5

P: n/a
> 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.

Dec 14 '05 #6

P: n/a
Ah, I was in a rush and misread the question. Sorry about that.

Chris

Dec 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.