Created
May 9, 2017 15:13
-
-
Save amis92/f2fe486fc31d30b6b24967c645bc5856 to your computer and use it in GitHub Desktop.
A bool that's neither true nor false :O
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; | |
using System.Runtime.InteropServices; | |
public class Test | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine($"false as byte: {new LolBoolUnion(false).ByteValue}"); | |
Console.WriteLine($"true as byte: {new LolBoolUnion(true).ByteValue}"); | |
Console.WriteLine($"0 as bool: {new LolBoolUnion(0).BoolValue}"); | |
Console.WriteLine($"128 as bool: {new LolBoolUnion(128).BoolValue}"); | |
Console.WriteLine($"128 == true: {new LolBoolUnion(128).BoolValue == true}"); | |
bool aBool = new LolBoolUnion(128).BoolValue; | |
switch (aBool) | |
{ | |
case true: | |
case false: | |
break; | |
default: | |
Console.WriteLine("LOL"); | |
break; | |
} | |
} | |
[StructLayout(LayoutKind.Explicit)] | |
public struct LolBoolUnion | |
{ | |
[FieldOffset(0)] | |
public readonly byte ByteValue; | |
[FieldOffset(0)] | |
public readonly bool BoolValue; | |
public LolBoolUnion(byte byteValue) : this() | |
{ | |
this.ByteValue = byteValue; | |
} | |
public LolBoolUnion(bool boolValue) : this() | |
{ | |
this.BoolValue = boolValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Runnable version :D https://ideone.com/uANhyZ