There is such a form of increasing and decreasing int

 int a = 0; a++; ++a; a--; --a; 

The question is: Is it possible to create similar operations for bool in user code? Or do you have to write some kind of wrapper for Boolean and use only it?

Sort of:

 class MainClass { static void Main() { bool x; x = false; Console.WriteLine(++x); x = false; Console.WriteLine(x++); Console.WriteLine(x); } } /* Output true false true */ 
  • describe how you imagine it - etki
  • @Etki Added a question - BwehaaFox
  • one
    @Etki I am interested in the possibility of implementation, the application I will find. - BwehaaFox
  • one
    In C #, you cannot overload operators for existing types. Extension methods can be used; You can create a wrapper type over an existing type. - kmv
  • one
    By the way, in the case of bool , you can use the expression a ^= true , which cyclically assigns the variable a values ​​true / false / true / false / ... - kmv

3 answers 3

You cannot override an increment in an existing type, but you can easily write a wrapper. Example:

 struct Bool : IEquatable<Bool> { bool value; public Bool(bool b) { value = b; } public static Bool operator ++ (Bool b) => Bool.True; public static Bool operator -- (Bool b) => Bool.False; public static bool operator true (Bool b) => b.value; public static bool operator false (Bool b) => !b.value; public static Bool operator | (Bool b1, Bool b2) => new Bool(b1.value || b2.value); public static Bool operator & (Bool b1, Bool b2) => new Bool(b1.value && b2.value); public static implicit operator Bool(bool v) => new Bool(v); public static readonly Bool True = new Bool(true); public static readonly Bool False = new Bool(false); public bool Equals(Bool other) => value == other.value; public static Bool operator == (Bool l, Bool r) => l.value == r.value; public static Bool operator != (Bool l, Bool r) => l.value != r.value; public override bool Equals(object obj) => (obj is Bool) && Equals((Bool)obj); public override int GetHashCode() => value.GetHashCode(); public override string ToString() => value.ToString(); } 

Example of use:

 class Program { static void Main(string[] args) { Bool x; x = false; Console.WriteLine(++x); x = Bool.False; Console.WriteLine(x++); Console.WriteLine(x); Bool b1 = Bool.False; if (++b1) Console.WriteLine("preincreement true"); else Console.WriteLine("preincreement false"); Bool b2 = Bool.False; if (b2++) Console.WriteLine("postincreement true"); else Console.WriteLine("postincreement false"); if (ComputeFalse() && ComputeTrue()) { /* do nothing */ } var b3 = (b1 == b2); if (b3++) { /* do nothing */ } } static Bool ComputeTrue() { Console.WriteLine("computing true"); return Bool.True; } static Bool ComputeFalse() { Console.WriteLine("computing false"); return false; } } 

The output of the program:

True
False
True
preincreement true
postincreement false
computing false

    From the C # language specification (7.6.9):

    Unary operator overload resolution (§7.3.3). ++ and - exist for the following types: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal , and any type of enum.

    As you can see, the bool is absent in the list and this is understandable: the basic semantics for the increment and decrement of the Boolean flag does not exist. Therefore, you have to make your class and already in it to overload the operators.

      Well, in general for bool there is an operator! That inverts the value of a variable:

       bool x = true; bool y = !x; // false 
      • I know it. I need a logic similar to int , because there is a difference in writing ++a and a++ - BwehaaFox
      • four
        The difference between ++a and a++ is due to the fact that the values ​​of the integers are many, and they are different on both sides of the a. Boolean values ​​are only two, and if you want to get a value that is different from the current one, there is only one such value. - Igor
      • @Igor Updated the question. And let it may be meaningless and not necessary. I asked if it was possible to somehow overload operators for example or something like that to achieve this. - BwehaaFox
      • @BwehaaFox - understand you. There are no complaints about the issue. - Igor