locked
Not keyword in C#

    Question

  • Hi all,

    I am wondering if not keyword is in C#? For those who don't know what it does, it is the verbal form of ! Smile

    But the advantages over ! are:

    1. not is way easier to see than !. Because ! is very slim, and hard to notice.
    2. not is color coded like false, and that makes it easy to spot.
    3. ! is generally not seperated from what comes after, with a space, so you have to do it yourself.





    Thanks,
    Aw
    Wednesday, October 17, 2007 4:23 PM

Answers

  • Actualy to be entirely technical ! is the boolean compliment operator ( or logical negation operator )...

    To answer you question, no the keyword "not" is not part of C#.

    As for 3 that is mostly a stylistic thing for programmers, most of us with logic or other programming language background like the close association that doing !flag or !( a || b ). I know I would prefer ! as opposed to "not". Then again I do have some background in logic where not A is potentially everything else (ergo, not true would be anything but true, not necessarily just false).

    I know in VS 2005 at least you can have the editor color all the operators a different color than the standard text. Color coding is part of the GUI not the language itself.
    Wednesday, October 17, 2007 8:36 PM
  •  

    Hi Aw

     

    not (!) is not a restricted keyword in C#, just be precise.

    See all keywords here: http://msdn2.microsoft.com/en-us/library/x53a06bb(VS.71).aspx

     

    ! is an operator

    See: http://msdn2.microsoft.com/en-us/library/f2kd6eb2(VS.71).aspx

     

    Best regards

    - Jens

    Wednesday, October 17, 2007 8:36 PM
  •  Azurewrath wrote:
    Thanks guys. I didn't know what to call (!) so.


    MSDN docs call it "Logical Negation Operator" and it is overloadable, so it can be more than just a simple "not"

    at least it's not the the logic "not" sign: ¬
    or the integer compliment sign: ~
    Wednesday, October 17, 2007 9:04 PM
  • The VB "Not" operator has two C# equivalents:

    logical negation: !

    bitwise complement: ~

     

    In VB, 'Not' is used for both operations.

     

    Wednesday, October 17, 2007 10:42 PM
  • Wednesday, October 17, 2007 10:52 PM
  • Some other languages also have 'Not', but the C-based languages all use ! and ~.

     

    Wednesday, October 17, 2007 11:01 PM

All replies

  • Actualy to be entirely technical ! is the boolean compliment operator ( or logical negation operator )...

    To answer you question, no the keyword "not" is not part of C#.

    As for 3 that is mostly a stylistic thing for programmers, most of us with logic or other programming language background like the close association that doing !flag or !( a || b ). I know I would prefer ! as opposed to "not". Then again I do have some background in logic where not A is potentially everything else (ergo, not true would be anything but true, not necessarily just false).

    I know in VS 2005 at least you can have the editor color all the operators a different color than the standard text. Color coding is part of the GUI not the language itself.
    Wednesday, October 17, 2007 8:36 PM
  •  

    Hi Aw

     

    not (!) is not a restricted keyword in C#, just be precise.

    See all keywords here: http://msdn2.microsoft.com/en-us/library/x53a06bb(VS.71).aspx

     

    ! is an operator

    See: http://msdn2.microsoft.com/en-us/library/f2kd6eb2(VS.71).aspx

     

    Best regards

    - Jens

    Wednesday, October 17, 2007 8:36 PM
  • Thanks guys. I didn't know what to call (!) so. It's very hard to write stuff about ! and not, when they change the meaning of this text itself Smile

    I see but still putting not inside C# would be a nice addition IMO. Also doesn't (!) mean not too?




    Thanks,
    Aw
    Wednesday, October 17, 2007 8:45 PM
  •  Azurewrath wrote:
    Thanks guys. I didn't know what to call (!) so.


    MSDN docs call it "Logical Negation Operator" and it is overloadable, so it can be more than just a simple "not"

    at least it's not the the logic "not" sign: ¬
    or the integer compliment sign: ~
    Wednesday, October 17, 2007 9:04 PM
  • The VB "Not" operator has two C# equivalents:

    logical negation: !

    bitwise complement: ~

     

    In VB, 'Not' is used for both operations.

     

    Wednesday, October 17, 2007 10:42 PM
  • Thanks guys!

    David are you saying VB has not? Oh man Sad Is it capital?




    Thanks,
    Aw
    Wednesday, October 17, 2007 10:45 PM
  • Wednesday, October 17, 2007 10:52 PM
  • Thanks man. Since only vb has it, then it probably means we won't have it, right?




    Thanks,
    Aw
    Wednesday, October 17, 2007 10:54 PM
  • Some other languages also have 'Not', but the C-based languages all use ! and ~.

     

    Wednesday, October 17, 2007 11:01 PM
  • Thanks David.




    Aw
    Wednesday, October 17, 2007 11:27 PM
  • If you really, really, really want something like that in C#, you can always roll your own...

    public static bool Not(bool expr) { return !expr; }

     

    For your own code, if that is your preference, great!

    For code shared on a team, you'll probably notice people replacing Not(myStateFlag) with !myStateFlag behind your back.

    Thursday, November 10, 2011 1:32 PM
  • ((Yes, this thread is old, and yes; I'm still replying to it.))

    I've seen some people suggest that whenever you make a boolean property, you should also make one which is the logical negation of it - assuming that you use sensible names for the properties. This is precisely so that you *can* avoid using "!" in many cases, and also because it can make code more readable.

    So for example, if you had a property called IsEmpty you would also make one called IsNotEmpty.

    Then code like this:

    if (!container.IsEmpty)
        ...

    Becomes:

    if (container.IsNotEmpty)
        ...

    Similarly, something like Measurements.AreInRange() would also have a version called AreOutOfRange() so that

    if (!measurements.AreInRange())
        ...

    Becomes:

    if (measurements.AreOutOfRange())
        ...

    That is clearly more readable AND more like natural language. (I don't know about you, but in English I think that "something is not empty" rather than "not something is empty").

    The drawback is of course that you end up with double the number of boolean properties...


    Friday, November 11, 2011 1:43 PM
  • To answer you question, no the keyword "not" is not part of C#.


    That statment is a double negative, so you are saying that "not" is a part of C#.

    The statement should read:

    "To answer you question, the keyword "not" is not part of C#."

    Monday, November 28, 2011 11:46 AM