Do you avoid casts and use the "as operator" instead?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
No component provided for introEmbed
private void AMControlMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var auc = (AMUserControl)sender;
var aucSessionId = auc.myUserControl.Tag;
// snip snip snip
}

❌ Figure: Bad example

private void AMControlMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var auc = sender as AMUserControl;
if (auc != null)
{
var aucSessionId = auc.myUserControl.Tag;
// snip snip snip
}
}

✅ Figure: Good example

More info here: http://blog.gfader.com/2010/08/avoid-type-casts-use-operator-and-check.html

Acknowledgements

Adam Cogan
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star