新的 C# 类型是否应该是“动态”?被利用?
可能的重复:
为什么在 C# 中使用动态类型?
新的 C# 类型“dynamic”应该是现在经常使用编译安全的类型安全变量? 或者它应该只用于与 JavaScript 等动态语言集成,或者用于“魔术字符串”的领域 用于从 XML 等访问数据,这些数据一开始就不是类型安全的?
Possible Duplicate:
Why use dynamic typing in c#?
Should the new C# type 'dynamic' be used now much in place of Type safe variables that are compile safe?
Or should it only be used in to integrate with dynamic languages like JavaScript or in areas where 'magic strings'
where used to access data from XML ect.. which aren't type safe to begin with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“我个人认为,动态类型在 C# 4 中相对很少有用 - 基本上当您处理已经只能动态知道的数据时,例如反射或导航 XML。” - Jon Skeet 此处
对于详细版本: 在这里试试
"My personal belief is that dynamic typing will be relatively rarely useful in C# 4 - basically when you're dealing with data which is already only known dynamically, e.g. reflection, or navigating XML." - Jon Skeet here
For the elaborated version: try here
我会说避免它。让静态类型语言保持静态类型。不幸的是,有些人会使用它,而你将不得不处理他们的代码。 C# 为您提供了编译时检查以供使用。如果放弃的话就太浪费了。
如果您觉得想要经常使用
dynamic
,那么就去编写 Ruby 或 Python。这就是您想要解决问题的方式,而且肯定有一种语言可以提供这种类型的解决方案。I would say avoid it. Let the statically typed language remain statically typed. Unfortunately, some bloke is going to use it, and you're going to have to deal with their code. C# gave you compile time checking to use. It'd be a waste to let it go.
If you feel you want to use
dynamic
a lot, go write Ruby or Python. It's just the way you want to attack the problem, and there's definitely a language for that type of solution.