as operator performs a downcast that evaluates to null rather than throwing an exception if the downcast fails:
Suppose we have the following class
class Shape{ public string Name; } class Circle : Shape // inherits from Shape { public long Radius; } class Square : Shape // inherits from Shape { public decimal Width; }
For the following code
Shape a = new Shape(); Circle s = a as Circle; // s is null; no exception thrown
This is useful when you're going to subsequently test whether the result is null:
if (s != null)
Console.WriteLine (s.Radius);