静态隐式运算符 [英] Static implicit operator

查看:76
本文介绍了静态隐式运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近找到了以下代码:

I recently found this code:

 public static implicit operator XElement(XmlBase xmlBase)
 {
     return xmlBase.Xml;
 }

static implicit operator是什么意思?

推荐答案

这是 转换运算符 .这意味着您可以编写以下代码:

This is a conversion operator. It means that you can write this code:

XmlBase myBase = new XmlBase();
XElement myElement = myBase;

编译器不会抱怨!在运行时,将执行转换运算符-将myBase作为参数传递,并返回有效的XElement作为结果.

And the compiler won't complain! At runtime, the conversion operator will be executed - passing myBase in as the argument, and returning a valid XElement as the result.

这是您作为开发人员告诉编译器的一种方式:

It's a way for you as a developer to tell the compiler:

即使它们看起来像是两种完全不相关的类型,实际上也存在一种从一种转换为另一种的方法;让我来处理如何做到这一点的逻辑."

"even though these look like two totally unrelated types, there is actually a way to convert from one to the other; just let me handle the logic for how to do it."

这篇关于静态隐式运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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