在类型之间转换 [英] Converting between types

查看:92
本文介绍了在类型之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为70-536考试学习,现在我正在检查有关converting between types的课程,我对此表示怀疑.

I'm studying for the 70-536 exam and now I'm checking the lesson about converting between types and I have doubts.

总是隐式转换,这是扩大的转换吗?和显式转换是缩小的转换?

Always implicit conversion it's a widening conversion? and Explicit conversion it's a narrowing conversion?

这是否也被认为是缩小转换?

Also this is consider an Narrowing conversion?

System.Int32 number32 = 25548612
System.Int16 number16 = (System.Int16) number32;     

推荐答案

缩小转换应该是明确的,而转换转换可能是隐式的,这只是一个设计准则.可以使用用户定义的转换来创建违反此准则的转换.每当类型实现隐式转换时,也可以使用显式转换.

That narrowing conversions should be explicit, and widening conversions may be implicitly is only a design guideline. It is possible to create conversions that violate this guideline with user defined conversions. It's also possible to use an explicit conversion whenever the types implemented the implicit conversion.

  • 扩大转化是指一种原始类型为每个值可以在结果类型中表示的转化.
  • 缩小转换范围是指在结果类型中无法表示原始类型一些某些值的转换.
  • A widening conversion is a conversion where every value of the original type can be represented in the result type.
  • A narrowing conversion is a conversion where some values of the original type cannot be represented in the result type.

由于某些Int32的值不能表示为Int16,因此这是一个缩小的转换.根据编译器选项的不同,Int16范围之外的值可能会溢出或引发异常.

Since some values of Int32 cannot be represented as Int16, this is a narrowing conversion. Depending on the compiler options, values outside the range of Int16 either overflow, or throw an exception.

与我之前所说的相反,这个概念也适用于基类和派生类之间的转换.

Contrary to what I previously said, this concept also applies to conversions between base- and derived classes.

您需要将事物类型作为可能的值的集合.而不是关于他们拥有哪些成员.

You need to thing of types as sets of possible values. And not about which members they have.

派生类的每个实例始终是基类变量的有效值.因此,从派生类到基类的转换正在扩大,因此是隐式的.

Every instance of the derived class is always a valid value of a variable of the base class. So casting from the derived class to the base class is widening, and thus implicit.

基类的某些实例不是派生类的有效值(例如,它们是从其他子树派生的,或者是基类本身的实例).因此,从基类到派生类的转换是狭窄的,因此是显式的.

Some instances of the base class are not valid values of the derived class(For example they derive from a different subtree, or are instances of the base class itself). So casting from the base class to the derived class is narrowing, and thus explicit.

有一些隐式转换,只是在松散的意义上扩大了,这种转换是有损耗的.

There are some implicit conversions, that are only widening in a loose sense, where the conversion is lossy.

尤其是int/Int32float/Singlelong/Int64double/Double.通过这些转换,某些输入值只能在结果类型中近似表示.

In particular int/Int32 to float/Single and long/Int64 to double/Double. With these conversions some input values can only be represented approximately in the result type.

您需要将类型视为一组允许的值.然后,您会看到派生类的每个实例也是基类的允许值.因此,从派生类到基类的转换正在扩大.

You need to look at types as a set of allowed values. Then you see that every instance of the derived class, is also an allowed value for the base class. Thus the conversion from derived to base class is widening.

相反,有些基类的值不是派生类的合法值.

And conversely there are values of the base class, that are not legal values of the derived class.

这篇关于在类型之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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