fork download
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. Console.WriteLine($"false as byte: {new LolBoolUnion(false).ByteValue}");
  9. Console.WriteLine($"true as byte: {new LolBoolUnion(true).ByteValue}");
  10. Console.WriteLine($"0 as bool: {new LolBoolUnion(0).BoolValue}");
  11. Console.WriteLine($"128 as bool: {new LolBoolUnion(128).BoolValue}");
  12. Console.WriteLine($"128 == true: {new LolBoolUnion(128).BoolValue == true}");
  13. bool aBool = new LolBoolUnion(128).BoolValue;
  14. switch (aBool)
  15. {
  16. case true:
  17. case false:
  18. break;
  19. default:
  20. Console.WriteLine("LOL");
  21. break;
  22. }
  23. }
  24.  
  25. [StructLayout(LayoutKind.Explicit)]
  26. public struct LolBoolUnion
  27. {
  28. [FieldOffset(0)]
  29. public readonly byte ByteValue;
  30. [FieldOffset(0)]
  31. public readonly bool BoolValue;
  32.  
  33. public LolBoolUnion(byte byteValue) : this()
  34. {
  35. this.ByteValue = byteValue;
  36. }
  37. public LolBoolUnion(bool boolValue) : this()
  38. {
  39. this.BoolValue = boolValue;
  40. }
  41. }
  42. }
Success #stdin #stdout 0.02s 131648KB
stdin
Standard input is empty
stdout
false as byte: 0
true as byte: 1
0 as bool: False
128 as bool: True
128 == true: False
LOL