什么时候应该使用隐式转换? [英] When should I use implicit casting?

查看:144
本文介绍了什么时候应该使用隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当是可以安全使用隐式转换?

使用案例:我正在与一组需要被照顾特殊(对Marshal.ReleaseComObject)的COM对象。它是确定创建隐式转换回实际的COM对象的包装类包装?

什么是某些情况下,当我的不应该的使用隐式转换?

解决方案
  1. 您需要执行这一投了很多。
  2. 在没有办法避免的演员。
  3. 这是psented作为转换/投影功能不是越多越好重新$ P $。为了把它的另一种方式,它必须是同一个对象剧组后。
  4. 您可以往返原始对象。 (不含蓄,虽然。)
  5. 这不会乱用现有或未来可能出现的函数重载。

我平时总结这些点为从不,但讽刺的是你的用例实际上听起来像而去......

When is it safe to use implicit casting?

Use Case: I'm working with a set of com objects that need to be taken care of specially (Marshal.ReleaseComObject). Is it OK to create a wrapper class that implicitly converts back to the actual com object wrapped?

What are some situations when I shouldn't use implicit casting?

解决方案

  1. You need to perform this cast a lot.
  2. There isn't a way to avoid the cast.
  3. It's not better represented as a conversion/projection function. To put it another way, it's got to be "the same object" after the cast.
  4. You can round-trip to the original object. (Not implicitly, though.)
  5. It's not going to mess with existing or possible future function overloads.

I usually summarize these points as "never", but ironically your use case actually sounds like a goer...

这篇关于什么时候应该使用隐式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆