c#:“System.Object”和“object”之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1017282/
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
c#: difference between "System.Object" and "object"
提问by Paolo Tedesco
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? Or is it just a matter of style?
在 C# 中,System.Object
在代码中使用而不仅仅是object
,或者System.String
而不是string
等等有什么区别吗?还是只是风格问题?
Is there a reason why one form is preferrable to the other?
一种形式比另一种更受欢迎是否有原因?
采纳答案by Jon Skeet
string
is an alias for global::System.String
. It's simply syntactic sugar. The two are exactlyinterchangable in almost all cases, and there'll be no difference in the compiled code.
string
是 的别名global::System.String
。这只是语法糖。两者正好在几乎所有情况下可以互换的,而且会在编译的代码没有什么区别。
Personally I use the aliases for variable names etc, but I use the CLR type names for names in APIs, for example:
我个人使用别名作为变量名等,但我在 API 中使用 CLR 类型名称作为名称,例如:
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- When specifying an enum base underlying type, onlythe aliases can be used
nameof
禁止使用别名- 指定枚举基础类型时,只能使用别名
回答by Johnno Nolan
One is an alias to the other. Its down to style.
一个是另一个的别名。它的风格。
回答by Colin Pickard
string
is an alias for global::System.String
, and object
for global::System.Object
string
是 的别名global::System.String
,并且object
为global::System.Object
Providing you have using System;
in your class, String
/ string
and Object
/ object
are functionally identical and usage is a matter of style.
如果您using System;
在课堂上拥有String
/string
和Object
/object
在功能上相同,并且用法是风格问题。
(EDIT: removed slightly misleading quote, as per Jon Skeet's comment)
(编辑:根据 Jon Skeet 的评论,删除了略微误导性的引用)
回答by abatishchev
There are no difference. There is a number of types, called Primitive Data Typeswhich are threated by compiler in style you mentioned.
没有区别。有许多类型,称为原始数据类型,它们以您提到的风格受到编译器的威胁。
Capitalized naming style is ISO naming rule. It's more general, common; forces the same naming rules for all objects in the source without exceptions such C# compiler has.
大写的命名风格是 ISO 命名规则。它更普遍,更常见;对源代码中的所有对象强制使用相同的命名规则,C# 编译器没有例外。
回答by Omar Abid
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)
但要注意 String 和 string 之间有区别(c#区分大小写)
回答by Matthew Scharley
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
是string
.NET框架中的实现。
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 循环之间的差异)。
回答by Sorskoot
The objecttype is an alias for System.Object. The objecttype is used and shown as a keyword. I think it has something to do with legacy, but that's just a wild guess.
的对象类型是别名System.Object的。的对象类型被使用并且示出为关键字。我认为这与遗产有关,但这只是一个疯狂的猜测。
Have a look at this MSDNpage 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 键...
回答by Quarkly
object, int, longand boolwere provided as training wheelsfor engineers that had trouble adapting to the idea that the data types were not a fixed part of the language. C#, unlike the languages that went before it, has no limit on the number of data types you can add. The 'System' library provides a starter kit featuring such useful types as System.Int32, System.Boolean, System.Double, System.DateTimeand so on, but engineers are encouraged to add their own. 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).
object、int、long和bool是为那些难以适应数据类型不是语言固定部分的想法的工程师提供的培训轮子。与之前的语言不同,C# 对可以添加的数据类型数量没有限制。“系统”库提供了一个入门工具包,其中包含System.Int32、System.Boolean、System.Double、System.DateTime等有用类型等等,但鼓励工程师添加他们自己的。因为 Microsoft 对快速采用他们的新语言感兴趣,所以他们提供了别名,使其看起来更像 C 语言,但这些别名是一个完全一次性的特性(如果你删除了所有内置别名,可能更好)。
While StyleCop does enforce the use of the legacy C-style aliases, it is a blemish on an otherwise logical set of rules. As of yet, I've not heard a single justification for this rule (SA1121) that wasn't based on dogma. If you think SA1121 is logical, then why is there no buildin type for datetime?
虽然 StyleCop 确实强制使用旧的 C 样式别名,但它是对其他合乎逻辑的规则集的一个缺陷。到目前为止,我还没有听说过针对这条规则 (SA1121) 的任何理由不是基于教条的。如果您认为 SA1121 是合乎逻辑的,那么为什么datetime没有内置类型?