C#中的String和string有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:46:32  来源:igfitidea点击:

What is the difference between String and string in C#?

提问by Lance Fisher

Example (note the case):

示例(注意大小写):

string s = "Hello world!";
String s = "Hello world!";

What are the guidelines for the use of each? And what are the differences?

每个的使用指南是什么?有什么区别?

采纳答案by Derek Park

stringis an alias in C# for System.String.
So technically, there is no difference. It's like intvs.System.Int32.

string是 C# 中的别名,用于System.String.
所以从技术上讲,没有区别。这就像intvs.System.Int32.

As far as guidelines, it's generally recommended to use stringany time you're referring to an object.

就指南而言,通常建议string您在引用对象时使用。

e.g.

例如

string place = "world";

Likewise, I think it's generally recommended to use Stringif you need to refer specifically to the class.

同样,我认为String如果您需要专门引用该类,通常建议使用它。

e.g.

例如

string greet = String.Format("Hello {0}!", place);

This is the style that Microsoft tends to use in their examples.

这是微软在他们的例子中倾向于使用的风格。

It appears that the guidance in this area may have changed, as StyleCopnow enforces the use of the C# specific aliases.

看来这方面的指南可能已更改,因为StyleCop现在强制使用 C# 特定别名。

回答by Ronnie

System.Stringis the .NET string class - in C# stringis an alias for System.String- so in use they are the same.

System.String是 .NET 字符串类 - 在 C# 中string是 for 的别名System.String- 所以在使用中它们是相同的。

As for guidelines I wouldn't get too bogged down and just use whichever you feel like - there are more important things in life and the code is going to be the same anyway.

至于指导方针,我不会陷入困境,只是使用你喜欢的任何一种——生活中有更重要的事情,无论如何代码都是一样的。

If you find yourselves building systems where it is necessary to specify the size of the integers you are using and so tend to use Int16, Int32, UInt16, UInt32etc. then it might look more natural to use String- and when moving around between different .net languages it might make things more understandable - otherwise I would use string and int.

如果你发现自己构建的系统中,需要指定要使用的整数的大小,因此倾向于使用Int16Int32UInt16UInt32等那么它可能看起来更自然的使用String-和不同的.NET语言之间走动时,它可能让事情更容易理解——否则我会使用字符串和整数。

回答by urini

Lower case stringis an alias for System.String. They are the same in C#.

小写string是 的别名System.String。它们在C#.

There's a debate over whether you should use the System types (System.Int32, System.String, etc.) types or the C# aliases(int, string, etc). I personally believe you should use the C# aliases, but that's just my personal preference.

有过是否应使用系统类型(辩论System.Int32System.String等)类型或C# aliasesintstring,等)。我个人认为您应该使用C# aliases,但这只是我个人的喜好。

回答by Luke Foust

The best answer I have ever heard about using the provided type aliases in C# comes from Jeffrey Richter in his book CLR Via C#. Here are his 3 reasons:

我听过的关于在 C# 中使用提供的类型别名的最佳答案来自 Jeffrey Richter 在他的书CLR Via C# 中。以下是他的3个理由:

  • I've seen a number of developers confused, not knowing whether to use stringor Stringin their code. Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.
  • In C#, longmaps to System.Int64, but in a different programming language, longcould map to an Int16or Int32. In fact, C++/CLI does in fact treat long as an Int32. Someone reading source code in one language could easily misinterpret the code's intention if he or she were used to programming in a different programming language. In fact, most languages won't even treat longas a keyword and won't compile code that uses it.
  • The FCL has many methods that have type names as part of their method names. For example, the BinaryReadertype offers methods such as ReadBoolean, ReadInt32, ReadSingle, and so on, and the System.Converttype offers methods such as ToBoolean, ToInt32, ToSingle, and so on. Although it's legal to write the following code, the line with float feels very unnatural to me, and it's not obvious that the line is correct:
  • 我见过许多开发人员感到困惑,不知道在他们的代码中是使用string还是String。因为在 C# 中,字符串(关键字)完全映射到 System.String(FCL 类型),所以没有区别,两者都可以使用。
  • 在 C# 中,long映射到System.Int64,但在不同的编程语言中,long可以映射到Int16Int32。事实上,C++/CLI 确实将 long 视为Int32。如果他或她习惯于使用不同的编程语言进行编程,那么以一种语言阅读源代码的人很容易误解代码的意图。事实上,大多数语言甚至不会将long视为关键字,也不会编译使用它的代码。
  • FCL 有许多方法将类型名称作为方法名称的一部分。例如,BinaryReader类型提供ReadBooleanReadInt32ReadSingle等方法,System.Convert类型提供ToBooleanToInt32ToSingle等方法。虽然写下面的代码是合法的,但是带float的那行对我来说感觉很不自然,而且不明显该行是正确的:
BinaryReader br = new BinaryReader(...);
float val  = br.ReadSingle(); // OK, but feels unnatural
Single val = br.ReadSingle(); // OK and feels good

So there you have it. I think these are all really good points. I however, don't find myself using Jeffrey's advice in my own code. Maybe I am too stuck in my C# world but I end up trying to make my code look like the framework code.

所以你有它。我认为这些都是非常好的点。然而,我发现自己并没有在自己的代码中使用 Jeffrey 的建议。也许我太沉迷于我的 C# 世界,但我最终试图让我的代码看起来像框架代码。

回答by Mel

It's a matter of convention, really. stringjust looks more like C/C++ style. The general convention is to use whatever shortcuts your chosen language has provided (int/Int for Int32). This goes for "object" and decimalas well.

这是约定俗成的问题,真的。 string只是看起来更像 C/C++ 风格。一般约定是使用您选择的语言提供的任何快捷方式(int/Int for Int32)。这也适用于“对象” decimal

Theoretically this could help to port code into some future 64-bit standard in which "int" might mean Int64, but that's not the point, and I would expect any upgrade wizard to change any intreferences to Int32anyway just to be safe.

从理论上讲,这可能有助于将代码移植到某些未来的 64 位标准中,其中“int”可能意味着Int64,但这不是重点,我希望任何升级向导都会更改任何int引用以Int32确保安全。

回答by user3296

There is one difference- you can't use Stringwithout using System;beforehand.

有一个区别-String没有using System;事先就不能使用。

回答by Anthony Mastrean

I prefer the capitalized .NETtypes (rather than the aliases) for formatting reasons. The .NETtypes are colored the same as other object types (the value types are proper objects, after all).

.NET出于格式原因, 我更喜欢大写类型(而不是别名)。这些.NET类型的颜色与其他对象类型相同(毕竟值类型是正确的对象)。

Conditional and control keywords (like if, switch, and return) are lowercase and colored dark blue (by default). And I would rather not have the disagreement in use and format.

条件和控制关键字(如ifswitchreturn)是小写的,颜色为深蓝色(默认情况下)。我宁愿在使用和格式上没有分歧。

Consider:

考虑:

String someString; 
string anotherString; 

回答by Ishmael

Using System types makes it easier to port between C# and VB.Net, if you are into that sort of thing.

使用系统类型可以更轻松地在 C# 和 VB.Net 之间进行移植,如果您喜欢那种东西的话。

回答by TheSoftwareJedi

stringand Stringare identical in all ways (except the uppercase "S"). There are no performance implications either way.

string并且String在所有方面都相同(除了大写的“S”)。两种方式都没有性能影响。

Lowercase stringis preferred in most projects due to the syntax highlighting

string由于语法突出显示,在大多数项目中首选小写

回答by Hallgrim

stringis just an alias for System.String. The compiler will treat them identically.

string只是 的别名System.String。编译器会同等对待它们。

The only practical difference is the syntax highlighting as you mention, and that you have to write using Systemif you use String.

唯一的实际区别是您提到的语法突出显示,using System如果您使用String.