Skip to content

Instantly share code, notes, and snippets.

@amis92
Created May 9, 2017 15:13
Show Gist options
  • Save amis92/f2fe486fc31d30b6b24967c645bc5856 to your computer and use it in GitHub Desktop.
Save amis92/f2fe486fc31d30b6b24967c645bc5856 to your computer and use it in GitHub Desktop.
A bool that's neither true nor false :O
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;
}
}
}
@amis92
Copy link
Author

amis92 commented May 9, 2017

Runnable version :D https://ideone.com/uANhyZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment