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
指令以确定别名,但不被其他类型的人看?
在不使用
bool
指令的情况下使用int
,string
,System
等? 最佳答案
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
关键字就是一个很好的例子。其他示例是您的问题所询问的类型。
关于c# - 当缺少System指令时,如何解析类型别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39901416/