c# Use As , not is in
27/01/2010 Leave a comment
In C#, use as, not is. The is keyword is used to see whether a reference can be cast as particular type, but it doesn’t return a reference converted to that type. So usually, if you get a positive result from the is, the first thing you’ll do is a cast—effectively implementing the same cast twice. With the as keyword, a reference cast as the new type is returned if it’s valid and a null is returned if it’s not. You then can check for the null and do what you like. The asapproach is fully 50% faster than the is approach.