缺少 System 指令时如何解析类型别名?
How are type aliases resolved when the System directive is missing?
bool
、int
和string
(仅举几例),都是System
类型的别名。
它们都可以在不包含 System
指令的情况下使用。
然而,如果没有 System
指令,则不能使用它们别名的类型。
public class Test
{
public static void Main()
{
bool b; //valid
Boolean b2; //compiler error
int i; //valid
Int32 i2; //compiler error
string s; //valid
String s2; //compiler error
}
}
我几乎不知道为什么会这样。
编译器是否对这些常用类型进行了异常处理?
是否正在查看 System
指令以确定别名,
但没有被关注其他类型?
它总是安全的吗
在没有 System
指令的情况下使用 bool
、int
、string
等?
Is the compiler making an exception for these commonly used types?
是的。
Is the System directive being looked at to determine the aliasing, but not being looked at for the other types?
是的。好吧,有点。没有系统指令。有一个 using
指令,您可以使用它导入定义类型的 System
命名空间。
Is it always safe to use bool, int,string, etc without the System directive?
是的。
许多像 C# 这样的现代语言都深受 C++ 的启发,而 C++ 又深受 C 的启发。C 语言采取了将编译器和运行时完全分离的创新立场。 C 的编译器过去(现在)绝对不对标准库做任何假设。理论上,可以编写不包含标准库的 C 程序。
但是,像 C# 这样的语言承认在编译器和运行时之间具有一定程度的共谋具有重要的优点。许多 C# 关键字依赖于运行时的支持。 yield return
关键字就是一个很好的例子。其他示例是您的问题所询问的类型。
如果您参考 https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx,您会发现内置布尔类型是 System.Boolean
的别名,而不是 Boolean
的别名,即 完全合格.
所以当你写 int x;
时,它会别名为 System.Int32 x;
而不仅仅是 Int32
否则如果你定义了它们可能会发生冲突在您自己的命名空间中键入名为 Int32
的类型。
使用这些内置类型应该始终是安全的,您不会遇到与 C/C++ 有时相同的问题 "built in" 类型具有不同的基础类型,因此 return 不同根据平台调用 sizeof
时的值。
bool
、int
和string
(仅举几例),都是System
类型的别名。
它们都可以在不包含 System
指令的情况下使用。
然而,如果没有 System
指令,则不能使用它们别名的类型。
public class Test
{
public static void Main()
{
bool b; //valid
Boolean b2; //compiler error
int i; //valid
Int32 i2; //compiler error
string s; //valid
String s2; //compiler error
}
}
我几乎不知道为什么会这样。
编译器是否对这些常用类型进行了异常处理?
是否正在查看
System
指令以确定别名, 但没有被关注其他类型?它总是安全的吗 在没有
System
指令的情况下使用bool
、int
、string
等?
Is the compiler making an exception for these commonly used types?
是的。
Is the System directive being looked at to determine the aliasing, but not being looked at for the other types?
是的。好吧,有点。没有系统指令。有一个 using
指令,您可以使用它导入定义类型的 System
命名空间。
Is it always safe to use bool, int,string, etc without the System directive?
是的。
许多像 C# 这样的现代语言都深受 C++ 的启发,而 C++ 又深受 C 的启发。C 语言采取了将编译器和运行时完全分离的创新立场。 C 的编译器过去(现在)绝对不对标准库做任何假设。理论上,可以编写不包含标准库的 C 程序。
但是,像 C# 这样的语言承认在编译器和运行时之间具有一定程度的共谋具有重要的优点。许多 C# 关键字依赖于运行时的支持。 yield return
关键字就是一个很好的例子。其他示例是您的问题所询问的类型。
如果您参考 https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx,您会发现内置布尔类型是 System.Boolean
的别名,而不是 Boolean
的别名,即 完全合格.
所以当你写 int x;
时,它会别名为 System.Int32 x;
而不仅仅是 Int32
否则如果你定义了它们可能会发生冲突在您自己的命名空间中键入名为 Int32
的类型。
使用这些内置类型应该始终是安全的,您不会遇到与 C/C++ 有时相同的问题 "built in" 类型具有不同的基础类型,因此 return 不同根据平台调用 sizeof
时的值。