[英]C# 'is' operator Clarification
Does the is
operator indicate whether or not an object is an instance of a certain class, or only if it can be casted to that class? is
运算符是否指示 object 是否是某个 class 的实例,或者仅当它可以转换为该 class 时?
Assume I have a DbCommand
called command
that has actually has been initialized as a SqlCommand
.假设我有一个名为
command
的DbCommand
实际上已被初始化为SqlCommand
。 What is the result of command is OracleCommand
? command is OracleCommand
?
( SqlCommand
and OracleCommand
both inherit from DbCommand
) (
SqlCommand
和OracleCommand
都继承自DbCommand
)
It checks if the object is a member of that type, or a type that inherits from or implements the base type or interface.它检查 object 是否是该类型的成员,或者是从基类型或接口继承或实现的类型。 In a way, it does check if the object can be cast to said type.
在某种程度上,它确实检查了 object 是否可以转换为所述类型。
command is OracleCommand
returns false as it's an SqlCommand
, not an OracleCommand
. command is OracleCommand
返回 false 因为它是SqlCommand
,而不是OracleCommand
。 However, both command is SqlCommand
and command is DbCommand
will return true as it is a member of both of those types and can therefore be downcast or upcast to either respectively.但是,两个
command is SqlCommand
并且command is DbCommand
将返回 true,因为它是这两种类型的成员,因此可以分别向下转换或向上转换。
If you have three levels of inheritance, eg BaseClass
, SubClass
and SubSubClass
, an object initialized as new SubClass()
only returns true for is BaseClass
and is SubClass
.如果您有 inheritance 的三个级别,例如
BaseClass
、 SubClass
和SubSubClass
,则初始化为new SubClass()
的 object 仅对is BaseClass
和is SubClass
返回 true 。 Although SubSubClass
derives from both of these, the object itself is not an instance of it, so is SubSubClass
returns false.尽管
SubSubClass
派生自这两者,但 object 本身并不是它的实例,因此is SubSubClass
返回 false。
http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.80%29.aspx http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.80%29.aspx
An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
如果提供的表达式不为空,则 is 表达式的计算结果为 true,并且可以将提供的 object 强制转换为提供的类型,而不会引发异常。
is
indicate if the object can be casted to a class or interface.指示 object
is
可以转换为 class 或接口。
If you have a BaseClass and a SubClass then:如果你有一个 BaseClass 和一个 SubClass 然后:
var obj = new SubClass();
obj is SubClass
returns true; obj is SubClass
返回 true;
obj is BaseClass
also returns true; obj is BaseClass
也返回 true;
if(something is X) checks if the underlying type of something is X . if(something is X)检查某事物的基础类型是否为X 。 This is significantly different from checking if a type supports casting to X since many types can support casts to X without being of type X .
这与检查一个类型是否支持转换为X明显不同,因为许多类型可以支持转换为X而不是X类型。
Conversely the as operator attempts a conversion to a particular type and assigns null if source type is not within the inheritance chain of the target type.相反,如果源类型不在目标类型的 inheritance 链中,则as运算符尝试转换为特定类型并分配null 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.