Difference between the IS and AS keyword is C Sharp

1) The ‘is’ keyword returns a boolean value and is true if the object can be cast as the type.

if (p is BondPencil) return; // GET OUT OF HERE!

2) The ‘as’ keyword is similar to ‘is’ but ‘as’ returns a reference to the object if it can be cast or a null reference if it can not be cast.

if (null != p as BondPencil) return; // GET OUT OF HERE!

Taken from a post on Understanding Interfaces.

comments powered by Disqus