使用 object 与 Object 声明对象集合
可能的重复:
C# 中的字符串与字符串
我有一个简单的场景,我正在绑定网格将存储在列表中的对象的集合。我想知道的问题是声明对象列表的最佳实践是什么?
IList<object> myCollection;
或者
IList<Object> myCollection;
我读过一些代码标准文章,很多人建议使用 String 与 string,所以我想知道这里是否适用相同的规则以及为什么?这两种方法有什么不同(如果有的话),以及一种方法与另一种方法相比有什么性能提升。
该网格是自定义控件的一部分,其中列表作为将绑定到网格的属性公开。
Possible Duplicate:
String vs string in C#
I have a simple scenario where I am binding a grid to a collection of objects that will be stored in a list. The question that I was wondering is what is the best practice for declaring a list of objects?
IList<object> myCollection;
or
IList<Object> myCollection;
I've read some code standards articles and a lot of people suggest using String vs string so I was wondering if the same rule applies here and why? What is different about the two methods (if anything) and what kind of performance gains are there for doing it one way vs the other.
This grid is part of a custom control where the list is exposed as a property that will be bound to the grid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
string
和object
都是 C# 语言的关键字,分别解析为System.String
和System.Object
类。使用
object
相对于Object
的理论上优势是可以降低对基类库的依赖。然而,由于 BCL 是 .NET Framework 的核心部分,因此“实用”的“优势”是在简单情况下避免使用 System;。恕我直言,这种“优势”是有争议的。string
andobject
are keywords of the C# language and resolve toSystem.String
andSystem.Object
classes respectively.The theoretical advantage of using
object
overObject
is that you are lowering the dependency on the Base Class Library. However, since BCL is a core part of .NET Framework the practical “advantage” is avoidingusing System;
in simple cases. Such “advantage” is IMHO disputable.没有区别,小写对象是 Object 类的别名,因为小写字符串是 String 类的别名,int 是 Int32 等。
已经以最佳方式回答了:
C# 中的字符串与字符串
There's no difference, the lowercase object is an alias for the Object class, as the lower case string is an alias for the String class, int for Int32 etc.
already answered in the best way:
String vs string in C#
两者在运行时没有区别(它们编译为相同的 IL)。
唯一的区别是您是否按下 Shift 键并且有
using System;
或没有。There is no difference between the two at runtime (they compile to the same IL).
The only difference is if you press the shift key and have a
using System;
or not.在内部,它们受到相同的对待,因此性能或功能没有区别,这只是偏好问题。我实际上喜欢使用
string
而不是String
因为字符串的行为不像对象,所以我喜欢它们看起来不同(当然还有int
)Int32
)。我无法为自己辩护的奇怪之处在于,我也喜欢使用
object
,尽管对象的行为就像对象一样。Internally they are treated the same, so there is no difference to performance or functionality, it's just a matter of preference. I actually like using
string
instead ofString
as strings don't behave like objects, so I like that they look differently (and certainlyint
instead ofInt32
).The odd thing I can't defend for myself is that I like using
object
as well, even though objects behave as, well, Objects.