Showing posts with label Casting. Show all posts
Showing posts with label Casting. Show all posts

Monday, 20 June 2011

As Operator in C#



The as operator is used to perform certain types of conversions between compatible reference types. "As" Operator is also known as a safe cast. What is does is it attempts to cast from one type to another and if the cast fails it returns null instead of throwing an InvalidCastException.


As-casting is equivalent to the following expression except that expression is evaluated only one time.

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