Parker's DevEd Blog

Musings on all software development technologies, and education involving computer science and fraud investigation

C#: is vs. as

I have seen a lot of code lately using "is" instead of "as". I just want to get the word out there that it is much better to use "as" as it is more efficient. For example, here's a sample code post using "is":

if (someType is Point) {
    Point myPoint = someType as Point;   
    // Use myPoint as needed
}

This post actually casts the object stored in someType twice, to see if it is indeed a Point. Now, look at the following post using "as" which is more efficient. This only casts the object stored in someType once.

Point myPoint = someType as Point;
if (myPoint != null) {
    // Use myPoint as needed
}

Posted: Oct 08 2007, 04:40 PM by Parker.Hillius | with no comments
Filed under: , ,

Comments

No Comments