C# “as” and “is” keywords

“Is” is exactly what I have been looking for.  You use it to check the type of an object.  For example:

if(a is long)

Gabe Wishnie points out that “is” is much more efficient than:

if( a.GetType() == typeof(long))

“As” is a method of performing a typecast that gives you a null pointer rather than throwing an error if the object is the wrong type.

string a = b as string



Post a Comment

Required fields are marked *

%d bloggers like this: