c# - 当缺少System指令时,如何解析类型别名?

标签 c# types compiler-errors directive aliasing

boolintstring(仅举几例)都是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指令的情况下使用intstringSystem等?
  • 最佳答案

    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/

    相关文章:

    c# - 在 Visual Studio 解决方案中的某个项目上启用和禁用 "Step into"调试

    java - issueCommand(java.lang.String,int) 在 com.sun.mail.smtp.SMTPTransport 编译错误中具有私有(private)访问权限

    android - Google App Engine : DuplicateFileException

    go - 我们可以为 Go 中的错误创建子类型吗?

    java - 如何在解释的情况下使用java中的泛型类型声明?

    c# - 如何通过名称激活派生类型?

    r - 参数3使用 ‘mediate’包将多个形式参数与 ‘psych’函数匹配

    c# - 'HttpClientHandler' 不包含 'ClientCertificates' 在 net461 和 net47 中的定义?

    c# - 将后退按钮导航到另一个 Activity Xamarin C#

    c# - Entity Framework 中的排序列属性