繁体   English   中英

字符串别名在没有System.String的情况下工作但String不能

[英]string alias works without System.String But String does not

我今天遇到了一个奇怪的困惑。 虽然我在这篇文章中读到了C#中String和string之间的区别 :C#中String和string之间有什么区别? 但是当我尝试在不使用命名空间System情况下使用大写字母的String时,它无法识别。 喜欢

这段代码有效,

using System;

String s = "";

但是如果不使用System ,它就会出错。

而带有小写字母的string可以使用和不使用System命名空间。

如果String和string是相同的东西那么为什么一个只用它的命名空间和其他有和没有命名空间的工作。

string是一个C#构造,在编译期间将其转换System.String

每个C#程序默认都有:

using string = System.String;
using char = System.Char;
using int = System.Int32;
using double = System.Double;

System.String是类。 如果不能访问System命名空间,则不能使用String类型(因此,要么完全限定为System.String ,要么使用using System;指令)。

StringSystem.String的别名。 查看您在链接到的SO答案上获得728分数的答案,以获取一些别名的列表。 如果使用别名,它会自动抓取System. 位,所以你不需要明确地引用它,因为它已经是。

System.String是一个System命名空间的类。 字符串直接引用它。

键入string时,可以访问System.String的所有方法(和构造函数等)。

Nullable<T> :你可以写,例子

int? myInt = 2;

这是一回事

Nullable<int> myInt = 2;

只有Alias实用程序

好吧想想这个如下

假设我们有一个Namespace Foo,它有一个名为Boo的类。 现在如果我们需要调用Boo,我们需要使用“Using”关键字在项目中包含它。

如果我们为ex:Foo.Boo> NewBoo创建一个别名(不确定是否可能)

在这种情况下,编译器会将其视为Foo.Boo

这里是String case。

当我们编写编译为System.String的字符串时 ,因此我们不需要进行任何类型的System Namespace来访问字符串。

但是对于Just String,我们需要包含它。

实际上,如果不using System; ,这将无法编译using System; String name = "me";有一个波形String name = "me";

class Program
{

    static void Main(string[] args)
    {
        string name1 = "me";

        String name2 = "me";
    }

}

当我把鼠标放在string它说class System.String ,当我把它放在String它说the type or namespace String could not be found

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM