471,310 Members | 1,058 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Difference between type castings?

I'm starting to see a lot of
"somevar as sometype"

Just cause i'm curious, what's the difference between that and

(sometype) somevar

?
Nov 29 '05 #1
8 1121
The "as" operator is generally a safer way of performing type conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

--
Tim Wilson
..NET Compact Framework MVP

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:eL****************@tk2msftngp13.phx.gbl...
I'm starting to see a lot of
"somevar as sometype"

Just cause i'm curious, what's the difference between that and

(sometype) somevar

?

Nov 29 '05 #2
Hi Benny,

x = somevar as sometype
is equivalent to:
if somevar is sometype
x = (sometype) somevar;
else
x = null;

Thus, "somevar as sometype" never raises an InvalidCastException. If
the cast is illegal, null is returned instead.

Thi - http://thith.blogspot.com

Nov 29 '05 #3
Hi,

I would also like to add, that the "as" casting is not supported on
value types. For value types, you must always use the (cast) casting.

-Lenard

Truong Hong Thi wrote:
Hi Benny,

x = somevar as sometype
is equivalent to:
if somevar is sometype
x = (sometype) somevar;
else
x = null;

Thus, "somevar as sometype" never raises an InvalidCastException. If
the cast is illegal, null is returned instead.

Thi - http://thith.blogspot.com

Nov 29 '05 #4
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to know??

Thoughts?

Simon

Tim Wilson wrote:
The "as" operator is generally a safer way of performing type conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #5
Sure you do know if it is invalid. If returns a null reference if it is
not valid.

When you are getting out objects from a non-typed DataRow for example or
other typeless container, it is easier to use "as" than to put every
member access into a try block. Of course you can do that - I guess it
is also personal preference, which one you use.

-Lenard
Simon wrote:
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to know??

Thoughts?

Simon

Tim Wilson wrote:
The "as" operator is generally a safer way of performing type conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #6
Sorry, I shoudl have explained better. What if NULL IS the value. Do you
see what I mean?

Lenard Gunda wrote:
Sure you do know if it is invalid. If returns a null reference if it is
not valid.

When you are getting out objects from a non-typed DataRow for example or
other typeless container, it is easier to use "as" than to put every
member access into a try block. Of course you can do that - I guess it
is also personal preference, which one you use.

-Lenard
Simon wrote:
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to know??

Thoughts?

Simon

Tim Wilson wrote:
The "as" operator is generally a safer way of performing type
conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #7
Hi,

If you have a null value, you can cast that to anything you like. If
will not be an error. I don't think this would be a problem.

-Lenard

Simon wrote:
Sorry, I shoudl have explained better. What if NULL IS the value. Do you
see what I mean?

Lenard Gunda wrote:
Sure you do know if it is invalid. If returns a null reference if it
is not valid.

When you are getting out objects from a non-typed DataRow for example
or other typeless container, it is easier to use "as" than to put
every member access into a try block. Of course you can do that - I
guess it is also personal preference, which one you use.

-Lenard
Simon wrote:
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to
know??

Thoughts?

Simon

Tim Wilson wrote:

The "as" operator is generally a safer way of performing type
conversion.
From the help...

"The as operator is like a cast except that it yields null on
conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #8
Sorry I'll engage the brain first next time. Yup I get ya.

Cheers

Simon

Lenard Gunda wrote:
Hi,

If you have a null value, you can cast that to anything you like. If
will not be an error. I don't think this would be a problem.

-Lenard

Simon wrote:
Sorry, I shoudl have explained better. What if NULL IS the value. Do
you see what I mean?

Lenard Gunda wrote:
Sure you do know if it is invalid. If returns a null reference if it
is not valid.

When you are getting out objects from a non-typed DataRow for example
or other typeless container, it is easier to use "as" than to put
every member access into a try block. Of course you can do that - I
guess it is also personal preference, which one you use.

-Lenard
Simon wrote:

Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you
now don't know if the cast is invalid. Only safer if you don't need
to know??

Thoughts?

Simon

Tim Wilson wrote:

> The "as" operator is generally a safer way of performing type
> conversion.
> From the help...
>
> "The as operator is like a cast except that it yields null on
> conversion
> failure instead of raising an exception."
>
> http://msdn.microsoft.com/library/de...ml/vclrfas.asp
>
>

Nov 29 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Arne Claus | last post: by
39 posts views Thread by August Karlstrom | last post: by
6 posts views Thread by mthread | last post: by
10 posts views Thread by Ahmad Humayun | last post: by
21 posts views Thread by Nikolaus Rath | last post: by
reply views Thread by rosydwin | last post: by

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.