C/C++中没有提供bool类型,我们可以用0和非0取而代之;而在.Net中则提供了一个Boolean类型,其实是对0/1的一个封装。下面我们来看下这段代码:
Boolean b
=
true
;
Console.WriteLine(
sizeof
(Boolean));
//
得到的结果是1byte
unsafe
//
先要用/unsafe(项目属性->Build->Allow unsafe code)来关闭安全检查
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private bool m_value;
//省略其他成员的定义.....
}
MSDN中解释bool与Boolean的关系是:“bool 关键字是 System.Boolean 的别名”。观察上面的Boolean类型定义,在其继承的泛型接口和成员变量中,我们找到了“bool”的影子(我想可能时Reflector将IL代码反汇编时将类型Boolean翻译成C#中的关键字bool),也就是说Boolean的定义依赖与bool(Boolean)。这就好比鸡蛋与鸡,到底是先有鸡蛋还是先有鸡?到底是先有Boolean的类型定义还是先有Boolean类型呢?
附录 - Boolean类源码:
1
namespace System
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
using System.Globalization;
4
using System.Runtime.InteropServices;
5![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
7
public struct Boolean : IComparable, IConvertible, IComparable<bool>, IEquatable<bool>
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
internal const int True = 1;
10
internal const int False = 0;
11
internal const string TrueLiteral = "True";
12
internal const string FalseLiteral = "False";
13
private bool m_value;
14
private static char[] m_trimmableChars;
15
public static readonly string TrueString;
16
public static readonly string FalseString;
17
public override int GetHashCode()
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
if (!this)
20![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
return 0;
22
}
23
return 1;
24
}
25![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
public override string ToString()
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
if (!this)
29![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
return "False";
31
}
32
return "True";
33
}
34![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
public string ToString(IFormatProvider provider)
36![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
if (!this)
38![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
return "False";
40
}
41
return "True";
42
}
43![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
public override bool Equals(object obj)
45![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
if (obj is bool)
47![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
return (this == ((bool) obj));
49
}
50
return false;
51
}
52![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
public bool Equals(bool obj)
54![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
return (this == obj);
56
}
57![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
public int CompareTo(object obj)
59![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
if (obj != null)
61![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
if (!(obj is bool))
63![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeBoolean"));
65
}
66
if (this == ((bool) obj))
67![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
return 0;
69
}
70
if (!this)
71![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
return -1;
73
}
74
}
75
return 1;
76
}
77![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
public int CompareTo(bool value)
79![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
if (this == value)
81![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
return 0;
83
}
84
if (!this)
85![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
return -1;
87
}
88
return 1;
89
}
90![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
public static bool Parse(string value)
92![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
if (value == null)
94![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
throw new ArgumentNullException("value");
96
}
97
bool result = false;
98
if (!TryParse(value, out result))
99![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));
101
}
102
return result;
103
}
104![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
public static bool TryParse(string value, out bool result)
106![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
107
result = false;
108
if (value != null)
109![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
111![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
result = true;
113
return true;
114
}
115
if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
116![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
result = false;
118
return true;
119
}
120
if (m_trimmableChars == null)
121![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
char[] destinationArray = new char[string.WhitespaceChars.Length + 1];
123
Array.Copy(string.WhitespaceChars, destinationArray, string.WhitespaceChars.Length);
124
destinationArray[destinationArray.Length - 1] = '\0';
125
m_trimmableChars = destinationArray;
126
}
127
value = value.Trim(m_trimmableChars);
128
if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
129![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
result = true;
131
return true;
132
}
133
if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
134![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
result = false;
136
return true;
137
}
138
}
139
return false;
140
}
141![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142
public TypeCode GetTypeCode()
143![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
return TypeCode.Boolean;
145
}
146![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147
bool IConvertible.ToBoolean(IFormatProvider provider)
148![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
149
return this;
150
}
151![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
char IConvertible.ToChar(IFormatProvider provider)
153![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), new object[]
{ "Boolean", "Char" }));
155
}
156![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
sbyte IConvertible.ToSByte(IFormatProvider provider)
158![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
return Convert.ToSByte(this);
160
}
161![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
162
byte IConvertible.ToByte(IFormatProvider provider)
163![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
164
return Convert.ToByte(this);
165
}
166![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167
short IConvertible.ToInt16(IFormatProvider provider)
168![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
169
return Convert.ToInt16(this);
170
}
171![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
172
ushort IConvertible.ToUInt16(IFormatProvider provider)
173![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
return Convert.ToUInt16(this);
175
}
176![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
177
int IConvertible.ToInt32(IFormatProvider provider)
178![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
179
return Convert.ToInt32(this);
180
}
181![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
182
uint IConvertible.ToUInt32(IFormatProvider provider)
183![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
return Convert.ToUInt32(this);
185
}
186![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
187
long IConvertible.ToInt64(IFormatProvider provider)
188![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
189
return Convert.ToInt64(this);
190
}
191![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
192
ulong IConvertible.ToUInt64(IFormatProvider provider)
193![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
194
return Convert.ToUInt64(this);
195
}
196![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
197
float IConvertible.ToSingle(IFormatProvider provider)
198![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
199
return Convert.ToSingle(this);
200
}
201![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
202
double IConvertible.ToDouble(IFormatProvider provider)
203![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
204
return Convert.ToDouble(this);
205
}
206![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
207
decimal IConvertible.ToDecimal(IFormatProvider provider)
208![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
209
return Convert.ToDecimal(this);
210
}
211![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
212
DateTime IConvertible.ToDateTime(IFormatProvider provider)
213![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
214![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), new object[]
{ "Boolean", "DateTime" }));
215
}
216![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
217
object IConvertible.ToType(Type type, IFormatProvider provider)
218![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
219
return Convert.DefaultToType(this, type, provider);
220
}
221![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
222
static Boolean()
223![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
224
TrueString = "True";
225
FalseString = "False";
226
}
227
}
228
}