Skip to main content
additional trickiness note
Source Link
DPenner1
  • 1.1k
  • 10
  • 18
  • Uses the operator allowing a class to have a truth value to separately evaluate a and b truth values.
  • a & b = false is accomplished via code in implicit type casts.
  • Had to use & and go through int instead of && because unfortunately order of operations has implicit conversion to bool as higher priority to the true/false operators.
  • I've used boolean to mimic the Java keyword (and probably others), but you could use Boolean and shadow System.Boolean to try to trick those more familiar with C#.
  • As a bonus, if a = false, b = false, then a & b = true, so two wrongs make a right.
  • I recommend this as a bug finding exercise for Java programmers being introduced to C# (if you don't like them).
  • Uses the operator allowing a class to have a truth value to separately evaluate a and b truth values
  • a & b = false is accomplished via code in implicit type casts
  • Had to use & and go through int instead of && because unfortunately order of operations has implicit conversion to bool as higher priority to the true/false operators.
  • As a bonus, if a = false, b = false, then a & b = true, so two wrongs make a right.
  • I recommend this as a bug finding exercise for Java programmers being introduced to C# (if you don't like them).
  • Uses the operator allowing a class to have a truth value to separately evaluate a and b truth values.
  • a & b = false is accomplished via code in implicit type casts.
  • Had to use & and go through int instead of && because unfortunately order of operations has implicit conversion to bool as higher priority to the true/false operators.
  • I've used boolean to mimic the Java keyword (and probably others), but you could use Boolean and shadow System.Boolean to try to trick those more familiar with C#.
  • As a bonus, if a = false, b = false, then a & b = true, so two wrongs make a right.
  • I recommend this as a bug finding exercise for Java programmers being introduced to C# (if you don't like them).
added variant solution
Source Link
DPenner1
  • 1.1k
  • 10
  • 18

A variant

Using similar implicit conversions, we can use the native && operator, but I like this solution less as I've not found a way to not declare the initial variables false (or use negation), resulting in immediately suspicious looking code:

boolean a = false; boolean b = false; boolean a_and_b = a && b; if (a) System.Console.WriteLine("a is true"); if (b) System.Console.WriteLine("b is true"); if (a_and_b) System.Console.WriteLine("a and b is true"); else System.Console.WriteLine("a and b is false"); public struct boolean { private bool Value; public static implicit operator boolean(bool b) => new boolean { Value = b }; public static implicit operator bool(boolean b) => !b.Value; } 

dotnet fiddle 2

A variant

Using similar implicit conversions, we can use the native && operator, but I like this solution less as I've not found a way to not declare the initial variables false (or use negation), resulting in immediately suspicious looking code:

boolean a = false; boolean b = false; boolean a_and_b = a && b; if (a) System.Console.WriteLine("a is true"); if (b) System.Console.WriteLine("b is true"); if (a_and_b) System.Console.WriteLine("a and b is true"); else System.Console.WriteLine("a and b is false"); public struct boolean { private bool Value; public static implicit operator boolean(bool b) => new boolean { Value = b }; public static implicit operator bool(boolean b) => !b.Value; } 

dotnet fiddle 2

edited body
Source Link
DPenner1
  • 1.1k
  • 10
  • 18

C#

Operator overloadingdefinitions, but not & or &&:

boolean a = true; boolean b = true; boolean a_and_b = a & b; if (a) System.Console.WriteLine("a is true"); if (b) System.Console.WriteLine("b is true"); if (a_and_b) System.Console.WriteLine("a and b is true"); else System.Console.WriteLine("a and b is false"); // ideally, put this away in an innocuous file public class boolean { private bool Value; public boolean(bool b) { Value = b; } public boolean(int i) { Value = i != 0; } public static implicit operator int(boolean b) => b.Value ? 0 : 1; public static implicit operator boolean(int i) => new boolean(i); public static implicit operator boolean(bool b) => new boolean(b); public static bool operator true(boolean b) => b.Value; public static bool operator false(boolean b) => b.Value; } 

dotnet fiddle

Notes

  • Uses the operator allowing a class to have a truth value to separately evaluate a and b truth values
  • a & b = false is accomplished via code in implicit type casts
  • Had to use & and go through int instead of && because unfortunately order of operations has implicit conversion to bool as higher priority to the true/false operators.
  • As a bonus, if a = false, b = false, then a & b = true, so two wrongs make a right.
  • I recommend this as a bug finding exercise for Java programmers being introduced to C# (if you don't like them).

C#

Operator overloading, but not & or &&:

boolean a = true; boolean b = true; boolean a_and_b = a & b; if (a) System.Console.WriteLine("a is true"); if (b) System.Console.WriteLine("b is true"); if (a_and_b) System.Console.WriteLine("a and b is true"); else System.Console.WriteLine("a and b is false"); // ideally, put this away in an innocuous file public class boolean { private bool Value; public boolean(bool b) { Value = b; } public boolean(int i) { Value = i != 0; } public static implicit operator int(boolean b) => b.Value ? 0 : 1; public static implicit operator boolean(int i) => new boolean(i); public static implicit operator boolean(bool b) => new boolean(b); public static bool operator true(boolean b) => b.Value; public static bool operator false(boolean b) => b.Value; } 

dotnet fiddle

Notes

  • Uses the operator allowing a class to have a truth value to separately evaluate a and b truth values
  • a & b = false is accomplished via code in implicit type casts
  • Had to use & and go through int instead of && because unfortunately order of operations has implicit conversion to bool as higher priority to the true/false operators.
  • As a bonus, if a = false, b = false, then a & b = true, so two wrongs make a right.
  • I recommend this as a bug finding exercise for Java programmers being introduced to C# (if you don't like them).

C#

Operator definitions, but not & or &&:

boolean a = true; boolean b = true; boolean a_and_b = a & b; if (a) System.Console.WriteLine("a is true"); if (b) System.Console.WriteLine("b is true"); if (a_and_b) System.Console.WriteLine("a and b is true"); else System.Console.WriteLine("a and b is false"); // ideally, put this away in an innocuous file public class boolean { private bool Value; public boolean(bool b) { Value = b; } public boolean(int i) { Value = i != 0; } public static implicit operator int(boolean b) => b.Value ? 0 : 1; public static implicit operator boolean(int i) => new boolean(i); public static implicit operator boolean(bool b) => new boolean(b); public static bool operator true(boolean b) => b.Value; public static bool operator false(boolean b) => b.Value; } 

dotnet fiddle

Notes

  • Uses the operator allowing a class to have a truth value to separately evaluate a and b truth values
  • a & b = false is accomplished via code in implicit type casts
  • Had to use & and go through int instead of && because unfortunately order of operations has implicit conversion to bool as higher priority to the true/false operators.
  • As a bonus, if a = false, b = false, then a & b = true, so two wrongs make a right.
  • I recommend this as a bug finding exercise for Java programmers being introduced to C# (if you don't like them).
deleted 2 characters in body
Source Link
DPenner1
  • 1.1k
  • 10
  • 18
Loading
Source Link
DPenner1
  • 1.1k
  • 10
  • 18
Loading