Boolean和bool VS. 鸡蛋和鸡



    C/C++中没有提供bool类型,我们可以用0和非0取而代之;而在.Net中则提供了一个Boolean类型,其实是对0/1的一个封装。下面我们来看下这段代码:
Boolean b =   true ;
Console.WriteLine(
sizeof (Boolean)); // 得到的结果是1byte
unsafe // 先要用/unsafe(项目属性->Build->Allow unsafe code)来关闭安全检查
... {
    
int i = (int)&b;//取地址
    Console.WriteLine(*(byte*)i);//上面得到bool类型占1byte,这里将Boolean中的值按byte来读取
}

    得出的结果是1,当Boolean变量b赋值为false时,得到的结果是0。虽然只用1个bit就可以表示0/1了,但计算机存储的最小单位是byte,没办法只好浪费另外7个bit了:)

 


    现在转入正题来扯淡,出于对Boolean的好奇,我用Reflector反汇编了下mscorlib.dll,得到Boolean的源代码(完整的代码见附录),居然看到了下面的内容:

     public   struct  Boolean : IComparable, IConvertible, IComparable < bool > , IEquatable < bool >
    
... {
        
private bool m_value;
        
//省略其他成员的定义.....
    }

    MSDN中解释bool与Boolean的关系是:“bool 关键字是 System.Boolean 的别名”。观察上面的Boolean类型定义,在其继承的泛型接口和成员变量中,我们找到了“bool”的影子(我想可能时Reflector将IL代码反汇编时将类型Boolean翻译成C#中的关键字bool),也就是说Boolean的定义依赖与bool(Boolean)。这就好比鸡蛋与鸡,到底是先有鸡蛋还是先有鸡?到底是先有Boolean的类型定义还是先有Boolean类型呢? 


附录 - Boolean类源码:

ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifnamespace System
  2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  3InBlock.gif    using System.Globalization;
  4InBlock.gif    using System.Runtime.InteropServices;
  5InBlock.gif
  6InBlock.gif    [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
  7InBlock.gif    public struct Boolean : IComparable, IConvertible, IComparable<bool>, IEquatable<bool>
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        internal const int True = 1;
 10InBlock.gif        internal const int False = 0;
 11InBlock.gif        internal const string TrueLiteral = "True";
 12InBlock.gif        internal const string FalseLiteral = "False";
 13InBlock.gif        private bool m_value;
 14InBlock.gif        private static char[] m_trimmableChars;
 15InBlock.gif        public static readonly string TrueString;
 16InBlock.gif        public static readonly string FalseString;
 17InBlock.gif        public override int GetHashCode()
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            if (!this)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 21InBlock.gif                return 0;
 22ExpandedSubBlockEnd.gif            }

 23InBlock.gif            return 1;
 24ExpandedSubBlockEnd.gif        }

 25InBlock.gif
 26InBlock.gif        public override string ToString()
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            if (!this)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 30InBlock.gif                return "False";
 31ExpandedSubBlockEnd.gif            }

 32InBlock.gif            return "True";
 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif
 35InBlock.gif        public string ToString(IFormatProvider provider)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            if (!this)
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 39InBlock.gif                return "False";
 40ExpandedSubBlockEnd.gif            }

 41InBlock.gif            return "True";
 42ExpandedSubBlockEnd.gif        }

 43InBlock.gif
 44InBlock.gif        public override bool Equals(object obj)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            if (obj is bool)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                return (this == ((bool) obj));
 49ExpandedSubBlockEnd.gif            }

 50InBlock.gif            return false;
 51ExpandedSubBlockEnd.gif        }

 52InBlock.gif
 53InBlock.gif        public bool Equals(bool obj)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55InBlock.gif            return (this == obj);
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58InBlock.gif        public int CompareTo(object obj)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            if (obj != null)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif                if (!(obj is bool))
 63ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 64InBlock.gif                    throw new ArgumentException(Environment.GetResourceString("Arg_MustBeBoolean"));
 65ExpandedSubBlockEnd.gif                }

 66InBlock.gif                if (this == ((bool) obj))
 67ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 68InBlock.gif                    return 0;
 69ExpandedSubBlockEnd.gif                }

 70InBlock.gif                if (!this)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 72InBlock.gif                    return -1;
 73ExpandedSubBlockEnd.gif                }

 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif            return 1;
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78InBlock.gif        public int CompareTo(bool value)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            if (this == value)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                return 0;
 83ExpandedSubBlockEnd.gif            }

 84InBlock.gif            if (!this)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                return -1;
 87ExpandedSubBlockEnd.gif            }

 88InBlock.gif            return 1;
 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif
 91InBlock.gif        public static bool Parse(string value)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 93InBlock.gif            if (value == null)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 95InBlock.gif                throw new ArgumentNullException("value");
 96ExpandedSubBlockEnd.gif            }

 97InBlock.gif            bool result = false;
 98InBlock.gif            if (!TryParse(value, out result))
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
100InBlock.gif                throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));
101ExpandedSubBlockEnd.gif            }

102InBlock.gif            return result;
103ExpandedSubBlockEnd.gif        }

104InBlock.gif
105InBlock.gif        public static bool TryParse(string value, out bool result)
106ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
107InBlock.gif            result = false;
108InBlock.gif            if (value != null)
109ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
110InBlock.gif                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
111ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
112InBlock.gif                    result = true;
113InBlock.gif                    return true;
114ExpandedSubBlockEnd.gif                }

115InBlock.gif                if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
116ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
117InBlock.gif                    result = false;
118InBlock.gif                    return true;
119ExpandedSubBlockEnd.gif                }

120InBlock.gif                if (m_trimmableChars == null)
121ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
122InBlock.gif                    char[] destinationArray = new char[string.WhitespaceChars.Length + 1];
123InBlock.gif                    Array.Copy(string.WhitespaceChars, destinationArray, string.WhitespaceChars.Length);
124InBlock.gif                    destinationArray[destinationArray.Length - 1= '\0';
125InBlock.gif                    m_trimmableChars = destinationArray;
126ExpandedSubBlockEnd.gif                }

127InBlock.gif                value = value.Trim(m_trimmableChars);
128InBlock.gif                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
129ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
130InBlock.gif                    result = true;
131InBlock.gif                    return true;
132ExpandedSubBlockEnd.gif                }

133InBlock.gif                if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
134ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
135InBlock.gif                    result = false;
136InBlock.gif                    return true;
137ExpandedSubBlockEnd.gif                }

138ExpandedSubBlockEnd.gif            }

139InBlock.gif            return false;
140ExpandedSubBlockEnd.gif        }

141InBlock.gif
142InBlock.gif        public TypeCode GetTypeCode()
143ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
144InBlock.gif            return TypeCode.Boolean;
145ExpandedSubBlockEnd.gif        }

146InBlock.gif
147InBlock.gif        bool IConvertible.ToBoolean(IFormatProvider provider)
148ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
149InBlock.gif            return this;
150ExpandedSubBlockEnd.gif        }

151InBlock.gif
152InBlock.gif        char IConvertible.ToChar(IFormatProvider provider)
153ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
154ExpandedSubBlockStart.gifContractedSubBlock.gif            throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), new object[] dot.gif"Boolean""Char" }));
155ExpandedSubBlockEnd.gif        }

156InBlock.gif
157InBlock.gif        sbyte IConvertible.ToSByte(IFormatProvider provider)
158ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
159InBlock.gif            return Convert.ToSByte(this);
160ExpandedSubBlockEnd.gif        }

161InBlock.gif
162InBlock.gif        byte IConvertible.ToByte(IFormatProvider provider)
163ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
164InBlock.gif            return Convert.ToByte(this);
165ExpandedSubBlockEnd.gif        }

166InBlock.gif
167InBlock.gif        short IConvertible.ToInt16(IFormatProvider provider)
168ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
169InBlock.gif            return Convert.ToInt16(this);
170ExpandedSubBlockEnd.gif        }

171InBlock.gif
172InBlock.gif        ushort IConvertible.ToUInt16(IFormatProvider provider)
173ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
174InBlock.gif            return Convert.ToUInt16(this);
175ExpandedSubBlockEnd.gif        }

176InBlock.gif
177InBlock.gif        int IConvertible.ToInt32(IFormatProvider provider)
178ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
179InBlock.gif            return Convert.ToInt32(this);
180ExpandedSubBlockEnd.gif        }

181InBlock.gif
182InBlock.gif        uint IConvertible.ToUInt32(IFormatProvider provider)
183ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
184InBlock.gif            return Convert.ToUInt32(this);
185ExpandedSubBlockEnd.gif        }

186InBlock.gif
187InBlock.gif        long IConvertible.ToInt64(IFormatProvider provider)
188ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
189InBlock.gif            return Convert.ToInt64(this);
190ExpandedSubBlockEnd.gif        }

191InBlock.gif
192InBlock.gif        ulong IConvertible.ToUInt64(IFormatProvider provider)
193ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
194InBlock.gif            return Convert.ToUInt64(this);
195ExpandedSubBlockEnd.gif        }

196InBlock.gif
197InBlock.gif        float IConvertible.ToSingle(IFormatProvider provider)
198ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
199InBlock.gif            return Convert.ToSingle(this);
200ExpandedSubBlockEnd.gif        }

201InBlock.gif
202InBlock.gif        double IConvertible.ToDouble(IFormatProvider provider)
203ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
204InBlock.gif            return Convert.ToDouble(this);
205ExpandedSubBlockEnd.gif        }

206InBlock.gif
207InBlock.gif        decimal IConvertible.ToDecimal(IFormatProvider provider)
208ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
209InBlock.gif            return Convert.ToDecimal(this);
210ExpandedSubBlockEnd.gif        }

211InBlock.gif
212InBlock.gif        DateTime IConvertible.ToDateTime(IFormatProvider provider)
213ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
214ExpandedSubBlockStart.gifContractedSubBlock.gif            throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), new object[] dot.gif"Boolean""DateTime" }));
215ExpandedSubBlockEnd.gif        }

216InBlock.gif
217InBlock.gif        object IConvertible.ToType(Type type, IFormatProvider provider)
218ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
219InBlock.gif            return Convert.DefaultToType(this, type, provider);
220ExpandedSubBlockEnd.gif        }

221InBlock.gif
222InBlock.gif        static Boolean()
223ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
224InBlock.gif            TrueString = "True";
225InBlock.gif            FalseString = "False";
226ExpandedSubBlockEnd.gif        }

227ExpandedSubBlockEnd.gif    }

228ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/happyhippy/archive/2007/04/12/710928.html

你可能感兴趣的:(Boolean和bool VS. 鸡蛋和鸡)