Last active
December 17, 2015 02:29
-
-
Save Porges/5536010 to your computer and use it in GitHub Desktop.
For StackOverflow: http://stackoverflow.com/a/16307237/10311
This file contains 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
class Program { | |
static bool M(out int x) | |
{ | |
x = 123; | |
return true; | |
} | |
static int N(EvilBool d) | |
{ | |
int y = 3; | |
if (d || M(out y)) | |
y = 10; | |
return y; | |
} | |
static void Main() | |
{ | |
var result = N(new EvilBool()); | |
// Prints 3! | |
Console.WriteLine(result); | |
} | |
} | |
class EvilBool | |
{ | |
private bool value; | |
public static bool operator true(EvilBool b) | |
{ | |
return b.Value; | |
} | |
public bool Value | |
{ | |
// Return true the first time this is called | |
// and false the second time | |
get | |
{ | |
value = !value; | |
return value; | |
} | |
} | |
public static bool operator false(EvilBool b) | |
{ | |
throw new NotImplementedException(); | |
} | |
public static implicit operator EvilBool(bool b) { return new EvilBool { value = b }; } | |
public static EvilBool operator|(EvilBool left, EvilBool right) | |
{ | |
return left.Value || right.Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment