动态与对象类型 [英] dynamic vs object type
问题描述
我交替使用了动态和对象类型.这两种类型有什么区别吗?使用一个而不是另一个有任何性能影响吗?其中哪一个更灵活?
I have used the dynamic and the object type interchangeably. Is there any difference between these two types? Is there any performance implications of using one over the other? Which one of these is more flexible?
推荐答案
它们非常不同.
如果您使用 dynamic
,您就选择了动态类型,从而在很大程度上选择退出编译时检查.是的,它的性能不如使用静态类型 使用静态类型.
If you use dynamic
you're opting into dynamic typing, and thus opting out of compile-time checking for the most part. And yes, it's less performant than using static typing where you can use static typing.
然而,无论如何你不能对 object
类型做太多事情——它几乎没有任何成员.你在哪里使用它?当您想编写可以处理多种类型的通用代码时,您通常应该考虑泛型而不是 object
.
However, you can't do much with the object
type anyway - it has hardly any members. Where do you find yourself using it? When you want to write general purpose code which can work with a variety of types, you should usually consider generics rather than object
.
这篇关于动态与对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!