为什么存在字符串的小写和大写版本,我应该使用哪些? [重复]

时间:2022-09-10 23:19:11

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),因为我更喜欢我的IDE中的颜色和外观而不是小写版本(string.Empty)...

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".

是否有一种“正确”的方式来使用这些不同或完全取决于个人偏好?我的假设是他们都是一样的,但说实话,我从来没有想过任何想法,直到今天无论如何我都想知道“如果它们都存在,它们必须都存在是有原因的”。

Is there 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?"

附: “完全重复”只回答问题的一半 - “哪个是对的?”,而不是“为什么它们都存在?”


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

确切重复:C#中字符串和字符串有什么区别?

Exact Duplicate: String vs string in C#

完全重复:C#中的字符串与字符串

5 个解决方案

#1


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等于System.String,int等于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做了类似的事情)。我想这两个原因是

  1. Habit. Get all these C and Java programmers to use C# by providing the same type names for some fundamental types.
  2. 习惯。通过为某些基本类型提供相同的类型名称,让所有这些C和Java程序员使用C#。

  3. Laziness: You don't have to import the System namespace to use them.
  4. 懒惰:您不必导入System命名空间即可使用它们。

EDIT Since 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.

但是,请考虑是否值得打破一个有助于统一项目代码的完善惯例。

#2


It is conceptually similar to something like this:

它在概念上类似于这样的东西:

using int=System.Int32

#3


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也是如此。

#4


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命名空间...

#5


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都是引用,而所有其他基类型都是值类型。在我看来,这是最明显的分离。

#1


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等于System.String,int等于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做了类似的事情)。我想这两个原因是

  1. Habit. Get all these C and Java programmers to use C# by providing the same type names for some fundamental types.
  2. 习惯。通过为某些基本类型提供相同的类型名称,让所有这些C和Java程序员使用C#。

  3. Laziness: You don't have to import the System namespace to use them.
  4. 懒惰:您不必导入System命名空间即可使用它们。

EDIT Since 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.

但是,请考虑是否值得打破一个有助于统一项目代码的完善惯例。

#2


It is conceptually similar to something like this:

它在概念上类似于这样的东西:

using int=System.Int32

#3


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也是如此。

#4


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命名空间...

#5


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都是引用,而所有其他基类型都是值类型。在我看来,这是最明显的分离。