简体   繁体   English

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

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

I faced a strange confusion today. 我今天遇到了一个奇怪的困惑。 Although I read the difference between String and string in C# in this post : What is the difference between String and string in C#? 虽然我在这篇文章中读到了C#中String和string之间的区别 :C#中String和string之间有什么区别? . But when I try to use String with capital letter without using the namespace System , it was not recognized. 但是当我尝试在不使用命名空间System情况下使用大写字母的String时,它无法识别。 Like 喜欢

This code works, 这段代码有效,

using System;

String s = "";

But without using System , it gives error. 但是如果不使用System ,它就会出错。

Whereas string with small letter works with and without using the System namespace. 而带有小写字母的string可以使用和不使用System命名空间。

If String and string are same things then why one works only with its namespace and other works with and without namespace as well. 如果String和string是相同的东西那么为什么一个只用它的命名空间和其他有和没有命名空间的工作。

string is a C# construct that translates , during compilation to System.String . string是一个C#构造,在编译期间将其转换System.String

Every C# program default has: 每个C#程序默认都有:

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

System.String is the class. System.String是类。 You can't use the type String without having access to the System namespace (so, either fully qualified as System.String , or with a using System; directive). 如果不能访问System命名空间,则不能使用String类型(因此,要么完全限定为System.String ,要么使用using System;指令)。

String is an alias for System.String . StringSystem.String的别名。 See the answer with 728 score on the SO answer you linked to for a list of some of the aliases. 查看您在链接到的SO答案上获得728分数的答案,以获取一些别名的列表。 If you use the alias, it automatically grabs the System. 如果使用别名,它会自动抓取System. bit, so you don't need to explicitly reference it, coz it already is. 位,所以你不需要明确地引用它,因为它已经是。

System.String is a class of System namespace. System.String是一个System命名空间的类。 string reference directly to it. 字符串直接引用它。

When you type string, you access to all methods (and constructors, etc) of System.String. 键入string时,可以访问System.String的所有方法(和构造函数等)。

The same thing with Nullable<T> : You can write, example Nullable<T> :你可以写,例子

int? myInt = 2;

It's the same thing of 这是一回事

Nullable<int> myInt = 2;

Only Alias utility 只有Alias实用程序

Well think of this as below 好吧想想这个如下

Lets say we have a Namespace Foo that has a Class called Boo. 假设我们有一个Namespace Foo,它有一个名为Boo的类。 Now If we need to call Boo we need to include that in the project using "Using" keyword. 现在如果我们需要调用Boo,我们需要使用“Using”关键字在项目中包含它。

In case we create an alias(Not sure whether it is possible or not) for ex : Foo.Boo > NewBoo 如果我们为ex:Foo.Boo> NewBoo创建一个别名(不确定是否可能)

in that case the compiler will see that as Foo.Boo 在这种情况下,编译器会将其视为Foo.Boo

Here in String case. 这里是String case。

When we write string that is compiled as System.String hence we do not need to do any sort of inclusion of the System Namespace to access string. 当我们编写编译为System.String的字符串时 ,因此我们不需要进行任何类型的System Namespace来访问字符串。

But in case of Just String we need to include it. 但是对于Just String,我们需要包含它。

It is actually true, this won't compile without using System; 实际上,如果不using System; ,这将无法编译using System; There was a squiggle in the line String name = "me"; String name = "me";有一个波形String name = "me";

class Program
{

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

        String name2 = "me";
    }

}

When I put mouse on string it says class System.String and when I put it on String it says the type or namespace String could not be found 当我把鼠标放在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