Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best "not is" syntax in C# [closed]

Tags:

syntax

c#

wpf

It is important to me that my syntax does not make other developers confused.

In this example, I need to know if a parameter is a certain type.

I have hit this before; what's the most elegant, clear approach to test "not is"?

Method 1:

void MyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if (!(e.parameter is MyClass)) { /* do something */ }
}

Method 2:

void MyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if (e.parameter is MyClass) { } else { /* do something */ }
}

Method 3:

void MyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    var _Parameter = e.parameter as MyClass;
    if (_Parameter != null) { /* do something */ }
}

Method 4:

void MyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    var _Type = typeof(MyClass);
    switch (e.parameter.GetType())
    { 
      case _Type: /* do nothing */; break;
      default: /* do something */; break;
    }
}

[EDIT] Method 5:

void MyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if ((e.parameter is MyClass) == false) { /* do something */ }
}

Which is the most straight-forward approach?

like image 439
Jerry Nixon Avatar asked Aug 09 '11 19:08

Jerry Nixon


1 Answers

This is obviously a matter of personal opinion and style, so there's no right answer, but I think this is clearest:

void MyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if ((e.parameter is MyClass) == false) { /* do something */ }
}

The == false is just more obvious than the !

like image 134
Steve Mallam Avatar answered Sep 22 '22 00:09

Steve Mallam