Mike-Ward.Net

Is "is" the forgotten C# keyword?

When is the last time you’ve seen or used the “is” keyword in code? It can often be used instead of the “as” keyword. Here’s an example from a Microsoft help file.

// CanExecuteRoutedEventHandler that only returns true if
// the source is a control.
public void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e)
{
    Control target = e.Source as Control;

    if(target != null)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}

The intent is plain enough. Return true only if the source routing the command is a Control. However, 10 lines of code (including white space) seems a bit excessive for so simple a test. The following one-liner does the same thing and it’s easier to understand.

public void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = (e.Source is Control);
}

What do you think?

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
Tweetz 1.1
Tweetz Desktop 1.0
Tweetz 0.11 Released
More... (1073)

Donate with PayPal