Ok Nice replies everyone, but lets get a bit practical. In your own code,ie non-vendor code the REAL power of AS keyword doesn't come to the fore.
But when dealing with vendor objects as in WPF/silverlight then the AS keyword is a real bonus.
for example if I have a series of controls on a Canvas and I want to track track thelast selectedControl, but clear the tracking varaible when I click the Canvas i would do this:
private void layoutroot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//clear the auto selected control
if (this.SelectedControl != null
&& sender is Canvas && e.OriginalSource is Canvas)
{
if ((sender as Canvas).Equals(( e.OriginalSource as Canvas)))
{
this.SelectedControl = null;
}
}
}
Another reason it use AS keyoword is when your Class implements 1 or more Interfaces and you want to explicitly use only one interface:
IMySecond obj = new MyClass as IMySecond
Although not really necessary here, it will assign null to variable obj if MyClass does not implement IMySecond