What is the result of using the "as operator" on a null object?

Why not try it?

You get a null value, no exception. Actually, the point of as is to never throw an exception.


It would return null. The as operator's purpose is to avoid throwing an exception, per MSDN:

The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.

For example:

object o = null; // also try with a string type
string result = o as string;
Console.WriteLine(result); // null

Tags:

C#