神奇的Boolean

   1:  

   2: // "true" or "false"

   3: public static string Bool2Str(bool b)

   4: {

   5:     switch (b)

   6:     {

   7:         case true:

   8:         return System.Boolean.TrueString;

   9:         case false:

  10:         return System.Boolean.FalseString;

  11:         default:

  12:         return "error";

  13:     }

  14: } 

  15:  

  16: Console.WriteLine(Bool2Str(true));

  17: Console.WriteLine(Bool2Str(false)); 

  18: unsafe

  19: {

  20:     bool val;

  21:     *((byte*)&val) = 2;

  22:     Console.WriteLine(val); 

  23:     Console.WriteLine(val==true); 

  24:     Console.WriteLine(Bool2Str(val));

  25: }

  26:  

大家可以猜一下这段代码会输出什么,我也是看了某篇文章的评论才知道会出现这么诡异的情况。。。

2 thoughts on “神奇的Boolean

Leave a Reply

Your email address will not be published. Required fields are marked *