当缺少System指令时,如何解析类型别名? [英] How are type aliases resolved when the System directive is missing?
问题描述
bool
, int
和 string
(仅举几例)都是 System
类型的别名.
它们都可以在不包含 System
指令的情况下使用.
但是,如果没有 System
指令,就不能使用它们别名的类型.
bool
,int
,andstring
(to name a few), are all aliases of System
types.
They can all be used without the System
directive being included.
Yet the types they alias can't be used without the System
directive.
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
}
}
对于这为何有效,我几乎没有疑问.
I have few questions as to why this works.
-
编译器是否为这些常用类型提供了例外?
Is the compiler making an exception for these commonly used types?
是否正在查看 System
指令以确定别名,但不被其他类型的人看?
Is the System
directive being looked at to determine the aliasing,
but not being looked at for the other types?
在没有 System
指令的情况下使用 bool
, int
, string
等?
Is it always safe to
use bool
, int
,string
, etc without the System
directive?
推荐答案
编译器是否为这些常用类型提供了例外?
Is the compiler making an exception for these commonly used types?
是的
是否正在查看System指令来确定别名,但没有查看其他类型的别名?
Is the System directive being looked at to determine the aliasing, but not being looked at for the other types?
是的.好吧,有点.没有系统指令.有一个 using
指令,您可以使用该指令导入定义类型的 System
命名空间.
Yes. Well, sort of. There is no System directive. There is a using
directive, with which you can import the System
namespace, which defines the types.
在没有System指令的情况下使用bool,int,string等是否总是安全的?
Is it always safe to use bool, int,string, etc without the System directive?
是的
像C#这样的许多现代语言都受到C ++的强烈启发,而C ++又受到C的启发.C语言采取了将编译器和运行时完全分离的创新立场.C的编译器绝对没有对标准库做任何假设.从理论上讲,可以编写不包含标准库的C程序.
Many modern languages like C# are heavily inspired from C++, which is in turn heavily inspired from C. The C language had taken the innovative stance of complete separation between compiler and runtime. The compiler of C was (is) making absolutely no assumptions about the standard libraries. In theory, it is possible to write C programs that do not include the standard libraries.
但是,像C#这样的语言都接受在编译器和运行时之间具有一定程度的共谋性是很重要的.许多C#关键字取决于运行时的支持.很好的例子就是 yield return
关键字.其他示例是您的问题所询问的类型.
However, languages like C# accept that there is significant merit in having a certain degree of collusion between the compiler and the runtime. Many C# keywords depend on support from the runtime. A very good example of that, the yield return
keyword. Other examples are the types that your question is asking about.
这篇关于当缺少System指令时,如何解析类型别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!