简体   繁体   English

c#:“ System.Object”和“ object”之间的区别

[英]c#: difference between “System.Object” and “object”

In C#, is there any difference between using System.Object in code rather than just object , or System.String rather than string and so on? 在C#中,在代码中使用System.Object而不是仅使用object ,或者使用System.String而不是string等有什么区别吗? Or is it just a matter of style? 还是仅仅是风格问题?

Is there a reason why one form is preferrable to the other? 有一种理由为什么一种形式优于另一种形式?

string is an alias for global::System.String . stringglobal::System.String的别名。 It's simply syntactic sugar. 它只是语法糖。 The two are exactly interchangable in almost all cases, and there'll be no difference in the compiled code. 两者在几乎所有情况下正好可以互换的,而且会在编译的代码没有什么区别。

Personally I use the aliases for variable names etc, but I use the CLR type names for names in APIs, for example: 就个人而言,我将别名用于变量名等,但是我将CLR类型名用于API中的名称,例如:

public int ReadInt32() // Good, language-neutral

public int ReadInt() // Bad, assumes C# meaning of "int"

(Note that the return type isn't really a name - it's encoded as a type in the metadata, so there's no confusion there.) (请注意,返回类型实际上不是名称,它在元数据中被编码为一种类型,因此这里没有混淆。)

The only places I know of where one can be used and the other can't (that I'm aware of) are: 我知道一个可以使用而另一个(我知道)不能使用的地方是:

  • nameof prohibits the use of aliases nameof禁止使用别名
  • When specifying an enum base underlying type, only the aliases can be used 指定枚举基础基础类型时, 只能使用别名

The object type is an alias for System.Object . 对象类型是System.Object的别名。 The object type is used and shown as a keyword. 使用对象类型并将其显示为关键字。 I think it has something to do with legacy, but that's just a wild guess. 我认为这与遗产有关,但这只是一个疯狂的猜测。

Have a look at this MSDN page for all details. 有关所有详细信息, 请参见此MSDN页面。

I prefer the use of the lowercased versions, but for no special reasons. 我更喜欢使用小写版本,但没有特殊原因。 Just because the syntax highlighting is different on these "basic" types and I don't have to use the shift key when typing... 只是因为这些“基本”类型上的语法突出显示是不同的,而且键入时我不必使用Shift键...

One is an alias to the other. 一个是另一个的别名。 Its down to style. 它的风格。

string is an alias for global::System.String , and object for global::System.Object stringglobal::System.String的别名,是global::System.Object object

Providing you have using System; 提供您using System; in your class, String / string and Object / object are functionally identical and usage is a matter of style. 在您的类中, String / stringObject / object在功能上是相同的,用法只是样式问题。

(EDIT: removed slightly misleading quote , as per Jon Skeet's comment) (编辑:根据乔恩·斯凯特的评论,删除了引人误解的报价

string (with the lowercase "s") is the string type of the C# language and the type System.String is the implementation of string in the .NET framework. string (小写的“ s”)是C#语言的字符串类型,类型System.String是.NET框架中string的实现。

In practise there is no difference besides stylistic ones. 在实践中,除了风格上没有什么区别。

EDIT: Since the above obviously wasn't clear enough, there is no difference between them, they are the same type once compiled. 编辑:由于上述内容显然不够清楚,它们之间没有区别,一旦编译它们就是同一类型。 I was explaining the semantic difference that the compiler sees (which is just syntactic sugar, much like the difference between a while and for loop). 我正在解释编译器看到的语义差异(这只是语法糖,很像while和for循环之间的差异)。

There are no difference. 没有区别。 There is a number of types, called Primitive Data Types which are threated by compiler in style you mentioned. 有许多类型称为原始数据类型 ,它们以您提到的样式受到编译器的威胁。

Capitalized naming style is ISO naming rule. 大写命名方式是ISO命名规则。 It's more general, common; 更一般,更常见; forces the same naming rules for all objects in the source without exceptions such C# compiler has. 对源中的所有对象强制使用相同的命名规则,而C#编译器则没有例外。

As of my knowledge, I know that it's a shortcut, it's easier to use string, rather than System.string. 据我所知,我知道这是一个快捷方式,使用字符串比使用System.string更容易。

But be careful there's a difference between String and string (c# is case sensitive) 但请注意,字符串和字符串之间有区别(C#区分大小写)

object , int , long and bool were provided as training wheels for engineers that had trouble adapting to the idea that the data types were not a fixed part of the language. 为难以适应数据类型不是语言固定部分的想法的工程师提供了objectintlongbool作为培训平台 C#, unlike the languages that went before it, has no limit on the number of data types you can add. C#与之前的语言不同,C#对您可以添加的数据类型数量没有限制。 The 'System' library provides a starter kit featuring such useful types as System.Int32 , System.Boolean , System.Double , System.DateTime and so on, but engineers are encouraged to add their own. “系统”库提供了一个入门工具包,其中包含诸如System.Int32System.BooleanSystem.DoubleSystem.DateTime等有用的类型,但鼓励工程师添加自己的类型。 Because Microsoft was interested in quick adoption of their new language, they provided aliases that made it appear as if the language was more 'C'-like, but these aliases are a completely disposable feature (C# would be just as good a language if you removed all the build-in aliases, probably better). 因为Microsoft对快速采用他们的新语言感兴趣,所以他们提供了别名,使它看起来像是更像“ C”的语言,但是这些别名是完全可抛弃的功能(如果您使用C#,它将是一种很好的语言。删除了所有内置别名,可能更好)。

While StyleCop does enforce the use of the legacy C-style aliases, it is a blemish on an otherwise logical set of rules. 尽管StyleCop确实强制使用了传统的C样式别名,但在其他逻辑上的规则集上却是瑕疵。 As of yet, I've not heard a single justification for this rule (SA1121) that wasn't based on dogma. 到目前为止,我还没有听说过没有基于教条的该规则(SA1121)的单一理由。 If you think SA1121 is logical, then why is there no buildin type for datetime ? 如果您认为SA1121是合乎逻辑的,那么为什么datetime没有内置类型?

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

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