为什么.Net Framework基本类型不包含IConvertible方法的实现? [英] Why does .Net Framework base types do not contain implementations of IConvertible methods?

查看:39
本文介绍了为什么.Net Framework基本类型不包含IConvertible方法的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net Framework基本类型,例如Int32,Int64,Boolean等.实现IConvertible接口,但是这些类型的元数据不包含IConvertible接口中定义的方法的实现,例如ToByte,ToBoolean等.

.Net Framework base types such as Int32, Int64, Boolean etc,. implement IConvertible interface but the metadata of these types do not contain the implementations of the methods defined in IConvertible interface such as ToByte, ToBoolean etc,.

我试图理解为什么基本类型即使实现IConvertible接口也没有方法实现.有人可以帮忙吗?

I am trying to understand why the base types do not have the method implementations even though they implements IConvertible interface. Could anyone please help on this?

推荐答案

仔细查看 明确 .

Take a closer look at the documentation - Int32 implements IConvertible explicitly.

当类/结构显式实现接口 时,您必须在调用这些方法之前将这种类型的实例强制转换为接口

When a class/struct implements an interface explicitly, you have to cast instances of that type to its interface before calling those methods

var asConvertable = (IConvertible) 3; //boxing
var someByte = asConvertible.ToByte();

这篇关于为什么.Net Framework基本类型不包含IConvertible方法的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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