csharp: the "as" keyword

Yet another C# irc channel question

23:22 [ mess] BackgroundWorker worker = sender as BackgroundWorker; < — what does this syntax do, specifically; "as"?
23:34 [ Flav] mess: for that event, the entity firing the event is an instance of BackgroundWorker – the method argument is “object” though, so you have to cast it as BackgroundWorker – “as” does that in a non-exception way – if sender isn’t an instance of BackgroundWorker, “worker” ends up null instead of (BackgroundWorker)sender which would throw InvalidCastException
23:35 [ mess] aha, thank you Flav

Advertisements