473,467 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What's difference between these casts?

I can do either:

string s = (string)someObject;

or

string s = someObject as string;

Then I can do:

int i = (int)someObject;

but I cannot do:

int i = someObject as int;

Why is there a difference? I thought the as keyword was just another way to
do a simple type cast? Or is there some benefit to using the as keyword which
leads to better performance over doing the traditional type cast, which value
types cannot take advantage of?
Nov 16 '05 #1
3 858

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com...
I can do either:

string s = (string)someObject;

or

string s = someObject as string;

Then I can do:

int i = (int)someObject;

but I cannot do:

int i = someObject as int;

Why is there a difference? I thought the as keyword was just another way
to
do a simple type cast? Or is there some benefit to using the as keyword
which
leads to better performance over doing the traditional type cast, which
value
types cannot take advantage of?


The difference between

T t = (T)o;
and
T t = o as T;

Is that the first one will throw in InvalidCastException if o is not, in
fact, of type T. The second will not throw an exception, and will leave t
set to null.

So then

int i = o as int;

must not compile since int is a ValueType and cannot be null.

David
Nov 16 '05 #2
MrNobody <Mr******@discussions.microsoft.com> wrote:
I can do either:

string s = (string)someObject;

or

string s = someObject as string;

Then I can do:

int i = (int)someObject;

but I cannot do:

int i = someObject as int;

Why is there a difference? I thought the as keyword was just another way to
do a simple type cast?
Not quite. It returns null if the value isn't of the appropriate type,
rather than throwing an exception. As you can't assign null to a value
type variable, it doesn't make sense to use the "as" operator with
value types.
Or is there some benefit to using the as keyword which
leads to better performance over doing the traditional type cast, which value
types cannot take advantage of?


"as" can lead to better performance. For instance:

if (x is Foo)
{
DoSomethingWith((Foo)x);
}

requires the type check to be done twice.

Foo y = x as Foo;
if (y != null)
{
DoSomethingWith(y);
}

requires the type check once, and a simple nullity check.

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

Thanks to the both of you!
Nov 16 '05 #4

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

Similar topics

16
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
2
by: Steven T. Hatton | last post by:
I am looking at the C++ MD5 implementation found on this site: http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html. I'm a bit unsure as to the necessity and/or effect of the various casts appearing...
4
by: nutty | last post by:
Hi, In my code I use two implicit casts in a row. VC compiled it in all version I have. 7.1 and 8.0, but it seems not to be standard. Comeau in strict mode and gcc don't accept it. Of course,...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
81
by: jacob navia | last post by:
Hi I am still writing my tutorial book about C. Here is the section about casts. I would be interested in your opinions about this. Some people have definite views about this subject ("never...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.