System.Boolean参考源网站上的的源代码指出的实例struct Boolean仅包含一个bool字段private bool m_value:
https://referencesource.microsoft.com/#mscorlib/system/boolean.cs,f1b135ff6c380b37
namespace System {
using System;
using System.Globalization;
using System.Diagnostics.Contracts;
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Boolean : IComparable, IConvertible
#if GENERICS_WORK
, IComparable<Boolean>, IEquatable<Boolean>
#endif
{
private bool m_value;
internal const int True = 1;
internal const int False = 0;
internal const String TrueLiteral = "True";
internal const String FalseLiteral = "False";
public static readonly String TrueString = TrueLiteral;
public static readonly String FalseString = FalseLiteral;
}
但我注意到...
bool是一个C#语言的别名为System.Boolean。- 类型是
struct Boolean值类型,这意味着它不能包含自己作为字段。 - ...但是此代码大概可以编译。
- 据我所知,当
-nostdlib编译器选项设置您需要提供自己的必不可少的类型定义一样System.String,System.Int32,System.Exception-这是唯一的区别。 - 已发布的源代码不包含其他特殊属性,例如
[MethodImpl( MethodImplOptions.InternalCall )]。
那么该代码如何编译?
bool是C#语言中的关键字。编译器和运行时都具有很多关于类型的内置知识,不需要System.Boolean的帮助。mscorlib中原始值类型的声明与该类型的框式表示形式匹配。