LA.NET [EN]

Jul 09

Ok, now that the PT Silverlight book is mostly done (I’m awaiting for feedback form my reviewers – if you’re one of them and you’re reading this post, then stop right now and go back to the manuscript please! :),,), I guess it’s time to return to my blog. And I’ll be writing some posts for my basic series, which is a topic that seems to make everyone happy :) – at least, I’ve received several emails about the previous posts, so I assume that this is a subject in which people are interested in.

We’ve all had to do some casting in the past, right? Everyone knows how to write a cast in C#: you just indicate use the ( ) operate, stating the type you want and that’s it. Here’s an example:

var aux = (MyClass)derived;

The previous code will compile, but you might end up getting an exception if derive isn’t a MyClass instance (or derived from it or if you don’t define a conversion operator in your derived’s class). So, how can you check if an object is of a specific type without getting an exception? Enter the is and as operators! The is operator will never throw an exception and it will return a Boolean which indicates if the object is of a specific type:

var isDerived = derived is MyClass;
if( isDerived ){
  //do something but cast before
  //ex.: ((MyClass)derived).CallSomeMethod();
}

You can use this, but I’m guessing that what you’d really love to have is a cast which doesn’t throw an exception when the type you’re casting to isn’t “compatible”. Enter the as operator:

var auxDerived = derived as MyClass;
if( auxDerived != null ){
  //do something
  //no need for casting
  //auxDerived.CallSomeMethod();
}

As you can see, the as operator will try to cast the object into the desired type. When that is possible, the result will automatically point to an instance of that type. When it isn’t, you’ll get null (that’s why you generally test the result of the operator against null before writing the code that accesses the members of the desired type).

Now, before you forget that the is operator exists, take a look at the following snippet:

public struct MyStruct { //more code }
var aux = instance as MyStruct;

Do you see anything wrong? No? Ok, let’s take a look at the docs:

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.

In other words, the previous snippet won’t compile because MyStruct is a struck. In fact, if you do read the docs, then you’ll notice that it  says that:

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 type

is equivalent to:

expression is type ? (type)expression : (type)null

In fact, you’ll be able to program without knowing this (since the compiler will enforce it at compilation time. However, you’ll really be amazed by the number of guys that says that they’re an “expert” in C# and don’t know about this behavior. And did I said that there was one C# “expert” that told me there was no way to “cast” a type into another if there wasn’t a relationship of is-a between them? I guess he didn’t had the time to read the section on converters in the C# spec…And no,I’m not really a C# expert. I’m just a curious guy which finds it interesting to write about this small things which aren’t used/known to many.

And that’s it for now. Stay tuned for more.

1 comment so far

  1. Joao Cardoso
    2:26 pm - 7-9-2010

    yeap. Same thing happens with TryCast in VB. People forget that it only works with ref types 😀 Small things can be as important as big ones :)