转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件使用 >> 系统软件 >> 正文
13.2 Explicit conversions         

13.2 Explicit conversions

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1498 更新时间:2009/4/25 0:44:45
The following conversions are classified as explicit conversions:
?All implicit conversions.
?Explicit numeric conversions.
?Explicit enumeration conversions.
?Explicit reference conversions.
?Explicit interface conversions.
?Unboxing conversions.
?User-defined explicit conversions.
Explicit conversions can occur in cast expressions (?4.6.6).
The set of explicit conversions includes all implicit conversions. [Note:
This means that redundant cast
expressions are allowed. end note]
The explicit conversions that are not implicit conversions are conversions
that cannot be proven to always
succeed, conversions that are known to possibly lose information, and
conversions across domains of types
sufficiently different to merit explicit notation.
13.2.1 Explicit numeric conversions
The explicit numeric conversions are the conversions from a numeric-type to
another numeric-type for
which an implicit numeric conversion (?3.1.2) does not already exist:
?From sbyte to byte, ushort, uint, ulong, or char.
?From byte to sbyte and char.
?From short to sbyte, byte, ushort, uint, ulong, or char.
?From ushort to sbyte, byte, short, or char.
?From int to sbyte, byte, short, ushort, uint, ulong, or char.
?From uint to sbyte, byte, short, ushort, int, or char.
?From long to sbyte, byte, short, ushort, int, uint, ulong, or char.
?From ulong to sbyte, byte, short, ushort, int, uint, long, or char.
?From char to sbyte, byte, or short.
?From float to sbyte, byte, short, ushort, int, uint, long, ulong, char,
or decimal.
?From double to sbyte, byte, short, ushort, int, uint, long, ulong, char,
float, or decimal.
C# LANGUAGE SPECIFICATION
116
?From decimal to sbyte, byte, short, ushort, int, uint, long, ulong, char,
float, or double.
Because the explicit conversions include all implicit and explicit numeric
conversions, it is always possible
to convert from any numeric-type to any other numeric-type using a cast
expression (?4.6.6).
The explicit numeric conversions possibly lose information or possibly
cause exceptions to be thrown. An
explicit numeric conversion is processed as follows:
?For a conversion from an integral type to another integral type, the
processing depends on the overflow
checking context (?4.5.12) in which the conversion takes place:
In a checked context, the conversion succeeds if the value of the source
operand is within the range of the
destination type, but throws a System.OverflowException if the value of the
source operand is outside
the range of the destination type.
In an unchecked context, the conversion always succeeds, and proceeds as
follows.
?If the source type is larger than the destination type, then the source
value is truncated by
discarding its ?extra? most significant bits. The result is then treated as
a value of the destination
type.
?If the source type is smaller than the destination type, then the source
value is either signextended
or zero-extended so that it is the same size as the destination type.
Sign-extension is
used if the source type is signed; zero-extension is used if the source
type is unsigned. The result
is then treated as a value of the destination type.
?If the source type is the same size as the destination type, then the
source value is treated as a
value of the destination type
?For a conversion from decimal to an integral type, the source value is
rounded towards zero to the
nearest integral value, and this integral value becomes the result of the
conversion. If the resulting
integral value is outside the range of the destination type, a
System.OverflowException is thrown.
?For a conversion from float or double to an integral type, the processing
depends on the overflowchecking
context (?4.5.12) in which the conversion takes place:
In a checked context, the conversion proceeds as follows:
?The value is rounded towards zero to the nearest integral value. If this


integral value is within
the range of the destination type, then this value is the result of the
conversion.
?Otherwise, a System.OverflowException is thrown.
In an unchecked context, the conversion always succeeds, and proceeds as
follows.
?The value is rounded towards zero to the nearest integral value. If this
integral value is within
the range of the destination type, then this value is the result of the
conversion.
?Otherwise, the result of the conversion is an unspecified value of the
destination type.
?For a conversion from double to float, the double value is rounded to the
nearest float value. If
the double value is too small to represent as a float, the result becomes
positive zero or negative zero.
If the double value is too large to represent as a float, the result
becomes positive infinity or negative
infinity. If the double value is NaN, the result is also NaN.
?For a conversion from float or double to decimal, the source value is
converted to decimal
representation and rounded to the nearest number after the 28th decimal
place if required (?1.1.6). If the
source value is too small to represent as a decimal, the result becomes
zero. If the source value is NaN,
infinity, or too large to represent as a decimal, a
System.OverflowException is thrown.
?For a conversion from decimal to float or double, the decimal value is
rounded to the nearest
double or float value. While this conversion may lose precision, it never
causes an exception to be
thrown.
13.2.2 Explicit enumeration conversions
The explicit enumeration conversions are:
?From sbyte, byte, short, ushort, int, uint, long, ulong, char, float,
double, or decimal to
any enum-type.
?From any enum-type to sbyte, byte, short, ushort, int, uint, long, ulong,
char, float,
double, or decimal.
?From any enum-type to any other enum-type.
An explicit enumeration conversion between two types is processed by
treating any participating enum-type
as the underlying type of that enum-type, and then performing an implicit
or explicit numeric conversion
between the resulting types. [Example: For example, given an enum-type E
with and underlying type of int,
a conversion from E to byte is processed as an explicit numeric conversion (
?3.2.1) from int to byte,
and a conversion from byte to E is processed as an implicit numeric
conversion (?3.1.2) from byte to
int. end example]
13.2.3 Explicit reference conversions
The explicit reference conversions are:
?From object to any reference-type.
?From any class-type S to any class-type T, provided S is a base class of
T.
?From any class-type S to any interface-type T, provided S is not sealed
and provided S does not
implement T.
?From any interface-type S to any class-type T, provided T is not sealed
or provided T implements S.
?From any interface-type S to any interface-type T, provided S is not
derived from T.
?From an array-type S with an element type SE to an array-type T with an
element type TE, provided all
of the following are true:
S and T differ only in element type. (In other words, S and T have the same
number of dimensions.)
Both SE and TE are reference-types.
An explicit reference conversion exists from SE to TE.
?From System.Array and the interfaces it implements, to any array-type.
?From System.Delegate and the interfaces it implements, to any
delegate-type.
The explicit reference conversions are those conversions between
reference-types that require run-time

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · 办公软件  · 系统软件
    · 常用软件  · 聊天工具
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台