The as operator is used to explicitly convert a value to a given reference
type using a reference conversion
or a boxing conversion. Unlike a cast expression (?4.6.6), the as operator
never throws an exception.
Instead, if the indicated conversion is not possible, the resulting value
is null.
In an operation of the form e as T, e must be an expression and T must be a
reference type. The type of the
result is T, and the result is always classified as a value. The operation
is evaluated as follows:
?If the compile-time type of e is the same as T, the result is simply the
value of e.
?Otherwise, if an implicit reference conversion (?3.1.4) or boxing
conversion (?3.1.5) exists from the
compile-time type of e to T, this conversion is performed and becomes the
result of the operation.
?Otherwise, if an explicit reference conversion (?3.2.3) exists from the
compile-time type of e to T, a
dynamic type check is performed:
If the value of e is null, the result is the value null with the
compile-time type T.
Chapter 14 Expressions
169
Otherwise, let R be the run-time type of the instance referenced by e. If R
and T are the same type, if R is a
reference type and an implicit reference conversion from R to T exists, or
if R is a value type and T is an
interface type that is implemented by R, the result is the reference given
by e with the compile-time type T.
Otherwise, the result is the value null with the compile-time type T.
?Otherwise, the indicated conversion is never possible, and a compile-time
error occurs.
The as operator only performs reference conversions and boxing conversions.
Other conversions, such as
user defined conversions, are not possible with the as operator and should
instead be performed using cast
expressions.
type using a reference conversion
or a boxing conversion. Unlike a cast expression (?4.6.6), the as operator
never throws an exception.
Instead, if the indicated conversion is not possible, the resulting value
is null.
In an operation of the form e as T, e must be an expression and T must be a
reference type. The type of the
result is T, and the result is always classified as a value. The operation
is evaluated as follows:
?If the compile-time type of e is the same as T, the result is simply the
value of e.
?Otherwise, if an implicit reference conversion (?3.1.4) or boxing
conversion (?3.1.5) exists from the
compile-time type of e to T, this conversion is performed and becomes the
result of the operation.
?Otherwise, if an explicit reference conversion (?3.2.3) exists from the
compile-time type of e to T, a
dynamic type check is performed:
If the value of e is null, the result is the value null with the
compile-time type T.
Chapter 14 Expressions
169
Otherwise, let R be the run-time type of the instance referenced by e. If R
and T are the same type, if R is a
reference type and an implicit reference conversion from R to T exists, or
if R is a value type and T is an
interface type that is implemented by R, the result is the reference given
by e with the compile-time type T.
Otherwise, the result is the value null with the compile-time type T.
?Otherwise, the indicated conversion is never possible, and a compile-time
error occurs.
The as operator only performs reference conversions and boxing conversions.
Other conversions, such as
user defined conversions, are not possible with the as operator and should
instead be performed using cast
expressions.
本文详细解析了C#中的as运算符工作原理及其使用场景。as运算符用于显式地将值转换为引用类型,不会抛出异常,而是返回null。文章探讨了不同情况下的类型转换过程。
911

被折叠的 条评论
为什么被折叠?



