473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Casting / AS

Does anyone know the difference between Casting and using "AS" ?
Nov 16 '05 #1
4 1270
C# programmers reference - as operator:
....
Remarks:
The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception. More formally, an expression of the
form:

expression as typeis equivalent to:

expression is type ? (type)expression : (type)nullexcept that expression is
evaluated only once.

Note that the as operator only performs reference conversions and boxing
conversions. The as operator cannot perform other conversions, such as
user-defined conversions, which should instead be performed using cast
expressions.
"John Sutor" <jo********@cinfin.com> wrote in
news:e8**************@TK2MSFTNGP09.phx.gbl...
Does anyone know the difference between Casting and using "AS" ?

Nov 16 '05 #2
John Sutor <jo********@cinfin.com> wrote:
Does anyone know the difference between Casting and using "AS" ?


See http://www.yoda.arachsys.com/csharp/faq/#cast.as

--
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
A good example of where you might use AS is when you're testing to see if an
instance implements a specific interface, and to get hold of that interface
instance at the same time.
eg.

IWalks walker = animal as IWalks;
if (walks!=null)
{
... do something with 'walker'
}

Which is a little bit more efficient (when the instance does support the
interface) than the equivalent code:

if (animal is IWalks)
{
IWalks walker = (IWalks)animal;
... do something with 'walker'
}

--
John Wood
EMail: first name, dot, second name at priorganize.com
"John Sutor" <jo********@cinfin.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Does anyone know the difference between Casting and using "AS" ?

Nov 16 '05 #4
I should point out that the line:
if (walks!=null) ....should have been:
if (walker!=null)

(before Mr Skeet points it out for me >:)

--
John Wood
EMail: first name, dot, second name at priorganize.com
"John Wood" <sp**@isannoying.com> wrote in message
news:eB**************@TK2MSFTNGP09.phx.gbl... A good example of where you might use AS is when you're testing to see if an instance implements a specific interface, and to get hold of that interface instance at the same time.
eg.

IWalks walker = animal as IWalks;
if (walks!=null)
{
... do something with 'walker'
}

Which is a little bit more efficient (when the instance does support the
interface) than the equivalent code:

if (animal is IWalks)
{
IWalks walker = (IWalks)animal;
... do something with 'walker'
}

--
John Wood
EMail: first name, dot, second name at priorganize.com
"John Sutor" <jo********@cinfin.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Does anyone know the difference between Casting and using "AS" ?


Nov 16 '05 #5

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.