I have -never- used as
or is
in C# or any language that supports the keyword.
What have you used it for?
I dont mean how do i use it i mean how have you actually need it?
I also got away with doing no typecasting in a fairly large c++ project (i was proud).
So considering i almost never typecast why do i need the keyword as
or is
?
I use it for cleanly getting data from a
DataReader
that might beDBNull
.or
Custom TypeConverters comes first to mind.
Second comes from a case where I have a shadow of an object (for tracking changes) which is inherited from a base class
If you ever develop on a project that offers a plugin interface then
as
andis
will quickly become your VERY best friends.Here is a post from Eric Lippert describing how "as" is used in C#:
http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx
I use as all the time. When i have to pull back a serialized object from session cache, I use as to determine if the serialized object is there and of the right type. I can avoid the program throwing an error, using the as operator and checking for null. If it's null, I know something is missing, and I can recreate the object and push it back into cache for the next time I need it.
You could accomplish the same result using the cast operator, but that adds the overhead of an exception, especially when you know that the serialized object isn't going to be there part of the time.
marc_s's answer is a little flawed, I see this code all the time so I want to emphasize the importance of the difference between these operators.
is
is a boolean test to determine if an object is assignable to a certain type.as
checks to see if an object is assignable to a certain type and if it is, it returns that object as that type, if not, it returns null. marc_s's answer is really doing thisIt is redundant to use
is
withas
. When ever you useas
just replace it with the expression above, it is equivalent. Useis
with direct casts()
oras
only by itself. A better way to write that example would be.Or if you really like
as
A real world example that I run into all the time is getting objects from a storage area that only return type object. Caching is a great example.
I use
as
much less in real code because it really only works for reference types. Consider the following codeas
fails because an int can't be null.is
works fine thoughI am sure there is also some speed difference between these operators, but for a real application the difference is negligible.
C# offer way to cast using the is and as operator. The is operator checks whether an object is compatible with a given type, and the result of the evaluation is a Boolean: true or false. The is operator will never throw an exception. The following code demonstrates:
If the object reference is null, the is operator always returns false because there is no object available to check its type.
The is operator is typically used as follows:
In this code, the CLR is actually checking the object’s type twice: the is operator first checks to see if o is compatible with the Employee type. If it is, then inside the if statement, the CLR again verifies that o refers to an Employee when performing the cast.
C# offers a way to simplify this code and improve its performance by providing an as operator:
In this code, the CLR checks if o is compatible with the Employee type, and if it is, as returns a non-null pointer to the same object. If o is not compatible with the Employee type,then the as operator returns null.