A silly little C# language feature request: .! operator

Tags: C#

When we step back for a moment from our SOAs and Factories and Polymorphism, we have to admit that at the root of much of our code lie a hell of a lot of simple if/else statements. Like it or not, they're what makes our program logic tick.

All too often, though, our if logic is reversed - rather than checking for a true condition, we check for false:

if (!myDictionary.Contains(key))
{ } 

This syntax, while very common, isn't really easy to parse. While I can express my required logic in words as "if my dictionary does not contain key", my syntax reads like the unintuitive "if not my dictionary does contains key" - I tend to think of my C# code in terms of natural languages. Usually English, even though it's not my native tongue.

So in order to bring C# more in line with my way of thought, I thought of having C# accept this sort of syntax:

if (myDictionary.!Contains(key))
{ } 

The .! operator is valid only before a boolean member, and inverts the return value, making it equivalent to the code above. Now the code reads the way it should and is just as easy to understand.

How does this sound? Any other supporters for this? Should I file a feature request on Connect?

19 Comments

  • Omer van Kloeten said

    Comp-Sci's logical arithmetic is mainly derived from Logic Theory, where the 'not' operator is prepended to whatever it is it's 'not'ing. In the case you describe, the value is myDictionary's Contains method. You don't have to be able to speak it, just be able to parse it in accordance to those rules :)
    It's just like the case where real life 'or' actually acts like a logical 'xor' and the logical 'or' like a real life 'and/or'.
    The 'x IS NOT NULL' operator in SQL, for instance, is downright confusing for someone who knows programming languages, who immediately looks for 'NOT x IS NULL', but there isn't one (mostly).
    Really, I just think it'd be confusing.

  • Carl Q. said

    Against it. Im all for code clarity, simplicity and efficiency. But making exception for every little details will give you something like VB6 where ByRef does not always mean ByRef, variant types, etc. You see, i read this : !myDictionary.Contains(key) as DOES NOT Contains(Key) after all, do you really need to remind yourself when reading that you are dealing with "myDictionnary". You really are dealing with the function, and its return value. You can do it in vb using "with", which is not so great... With x If (Not .Contains("aaa")) Then End If End With

  • Joe said

    You're an idiot. We have enough crap in the language as it is. Start adding this stuff and you might as well be writing obtuse Perl code.

  • Ayende Rahien said

    I hardly ever use the ! operator, preferring to use things like: (Dictionary.Contains(key)==False) For the simple reason of code readability. I can _read_ that statement. dic.!Contains(key) is very obscure and very hard to see

  • bsimser said

    Sorry, you're trying to apply english logic to programming logic? Not a chance. It looks odd to me and not intuitive, which is your original intent. I would prefer (like above) something like: if(false == Dictionary.Contains(key)) { } or better yet: if(Dictionary.DoesNotContain(key)) { }

  • AvnerK said

    Fair enough, I was only half serious anyway. Not many takers for the idea. Another one down the great drain of history. :) Oh, and ever notice how those people who post rude comments on blogs never seem to sign in with their email address?

  • DJ said

    Ever notice how people claim they were joking or half serious when confronted with a large number of opinions belittling theirs?

  • Nick Berardi said

    I personally use if (dictionary.Contains(key) == false) just because it reads like if the dictionary contains key is not there then do the follow. I like that you use natural language to write you code. I do the same exact thing, that is why I love the 'as' and 'foreach' keyword.

Comments have been disabled for this content.