C# 为什么存在小写和大写版本的字符串,我应该使用哪个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/452368/
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
Why do lowercase and uppercase versions of string exist and which should I use?
提问by BenAlabaster
Okay, this may be a dumb question, but I've not been able to find any information on it.
好吧,这可能是一个愚蠢的问题,但我找不到任何相关信息。
Are String.Empty and string.Empty the same? I always find myself gravitating towards using the upper case version (String.Empty) because I prefer the color and look of it in my IDE than the lower case version (string.Empty)...
String.Empty 和 string.Empty 是一样的吗?我总是发现自己倾向于使用大写版本 (String.Empty),因为与小写版本 (string.Empty) 相比,我更喜欢它在 IDE 中的颜色和外观……
Is there a "correct" way to use these that differ or is it entirely down to personal preference? It was my assumption that they're both the same, but to be honest, I never gave it any thought until for whatever reason today I wondered "If they both exist, they must both exist for a reason".
是否有一种“正确”的方式来使用这些不同的东西,还是完全取决于个人喜好?我假设它们是相同的,但老实说,直到今天出于某种原因我想知道“如果它们都存在,它们都必须存在一定的原因”之前,我从来没有考虑过。
Isthere a reason that anyone knows of? If so, what is it? Can anyone enlighten me?
是否有一个原因,任何人都知道?如果是,那是什么?任何人都可以启发我吗?
P.S. The "exact duplicates" only answer half of the question - "which is right?", not the "why do they both exist?"
PS“完全重复”只能回答问题的一半——“哪个是对的?”,而不是“为什么它们都存在?”
Exact Duplicate: What is the difference between String and string in C#?
完全重复:C# 中的字符串和字符串有什么区别?
Exact Duplicate: String vs string in C#
完全重复:C# 中的字符串与字符串
采纳答案by Konrad Rudolph
In C#, lower-case type names are aliases for the System.xxx
type names, e.g. string
equals System.String
and int
equals System.Int32
.
在 C# 中,小写类型名称是类型名称的别名System.xxx
,例如string
equalsSystem.String
和int
equals System.Int32
。
It's best practice to use these language aliases for the type names instead of their framework equivalent, for the sake of consistency. So you're doing it wrong. ;-)
为了保持一致性,最佳实践是将这些语言别名用于类型名称而不是它们的框架等效项。所以你做错了。;-)
As for a reason why they both exist, the .NET types exist because they are defined in a language-independent standard for the .NET libraries called CTS (common type system). Why C# defines these aliases is beyond me (VB does something quite similar). I guess the two reasons are
至于它们都存在的原因,.NET 类型之所以存在,是因为它们是在称为 CTS(通用类型系统)的 .NET 库的独立语言标准中定义的。为什么 C# 定义这些别名超出了我的理解(VB 做了一些非常相似的事情)。我想这两个原因是
- Habit. Get all these C and Java programmers to use C# by providing the same type names for some fundamental types.
- Laziness: You don't have to import the
System
namespace to use them.
- 习惯。通过为一些基本类型提供相同的类型名称,让所有这些 C 和 Java 程序员使用 C#。
- 懒惰:您不必导入
System
命名空间即可使用它们。
EDITSince many people seem to prefer the other notation let me point out that this is by no means unreasonable. A good case can actually be made for the usage of the CTS type names rather than C#'s keywords and some superficially good arguments are offered in the other answers. From a purity/style point of view I would probably concur.
编辑由于许多人似乎更喜欢其他符号,让我指出这绝不是不合理的。实际上可以为使用 CTS 类型名称而不是 C# 的关键字制作一个很好的案例,其他答案中提供了一些表面上很好的论据。从纯度/风格的角度来看,我可能会同意。
However, consider if this is worth breaking a well-established convention that helps to unify code across projects.
但是,请考虑这是否值得打破有助于跨项目统一代码的完善约定。
回答by Ed S.
It is conceptually similar to something like this:
它在概念上类似于以下内容:
using int=System.Int32
回答by Frederik Gheysels
string is mapped to the String class AFAIK, so they're the same.
string 映射到 String 类 AFAIK,因此它们是相同的。
The same is true for, for example int and Int32.
例如 int 和 Int32 也是如此。
回答by Arjan Einbu
They are both the same.
他们都是一样的。
Personally I prefer using the lowercase string, the "blue one", using the C# keyword instead of the .NET class name for the same reason I'm using int instead of Int32. Also, the lowercased one doesn't require inclusion of the System namespace...
我个人更喜欢使用小写字符串“蓝色字符串”,使用 C# 关键字而不是 .NET 类名,原因与我使用 int 而不是 Int32 的原因相同。此外,小写的不需要包含 System 命名空间...
回答by Shalmanese
Personally, I prefer to use String as both String and Object are references whereas all the other base types are value types. In my mind, that's the clearest separation.
就个人而言,我更喜欢使用 String,因为 String 和 Object 都是引用,而所有其他基本类型都是值类型。在我看来,这是最清晰的分离。