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

How to Choose the Right VPN Service
Two Handy Command Line Scripts
Tweetz 1.3.2
Tweetz 1.3 Release
Mithril Rising
Tweetz Release 1.2.4 - Screen Off Detection
Tweetz Desktop Support for 280 Characters
More... (1084)

Donate