Well – “as” is faster to type than a “real” cast using two brackets and has the same result. Really?
C# provides several ways to cast one type to another one. The two used in most cases are the “
([Targettype]) [Variable]” and the “
[Variable] as [Targettype]” notations. Many developers see them as equivalents, some even prefer “as” because it does not throw an exception when the cast fails.
Let’s say we have a variable called obj of type Object and we want to cast it to an IFoo so we can call IFoo.Bar:
object obj; //... (obj as IFoo).Bar();