Created
July 26, 2012 04:36
Unremovable "== true ? true : false"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(new Program().M()); | |
} | |
bool M() | |
{ | |
C myVar = new C(true); | |
return myVar == true ? true : false; | |
// Removing " == true" changes the output of the program from "False" to "True" | |
// Removing the ternary operator causes a build failure. | |
// Removing both causes a build failure. | |
// If myVar is a nullable bool, the "== true" cannot be removed, but the ternary | |
// can be removed.. | |
} | |
} | |
class C | |
{ | |
private bool field = false; | |
public C(bool param) | |
{ | |
this.field = param; | |
} | |
// Evil. This essentially negates c, making the "== true" check mandatory. | |
public static C operator ==(C c, bool other) | |
{ | |
return new C(c.field != other); | |
} | |
public static C operator !=(C c, bool other) | |
{ | |
return new C(c.field == other); | |
} | |
// Allows the ternary operator to work. Note the lack of a conversion from C to bool. | |
public static bool operator true(C c) | |
{ | |
return c.field; | |
} | |
public static bool operator false(C c) | |
{ | |
return !c.field; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment