简体   繁体   English

C#“is”关键字的使用模式

[英]C# Usage patterns for the “is” keyword

Whats the common and not so common usage patterns for the C# keywork 'is'. 什么是C#keywork的常见和不常见的使用模式'是'。 I recently used it to count the number of used cells in a typed array, (still stuck at the bottom with xsd generated classes, as xsd2code had a number of problems with IETF designed schema, hence no generics). 我最近使用它来计算类型化数组中已使用单元格的数量,(仍然在xsd生成的类中停留在底部,因为xsd2code在IETF设计的模式中存在许多问题,因此没有泛型)。

What other common and more importantly common usage patterns are offered. 还提供了其他常见且更重要的常见使用模式。

The 'is' keyword is useful for determine if an object is convertible to a type via a reference, boxing or unboxing conversion (C# lang spec 7.9.10). 'is'关键字对于确定对象是否可通过引用,装箱或拆箱转换(C#lang规范7.9.10)转换为类型非常有用。 It is similar to 'as' except that it doesn't actually do the conversion, merely return if it is possible. 它类似于'as',除了它实际上不进行转换,只是在可能的情况下返回。

It is useful in any scenario where knowing if an object is convertible to a type is useful but having the object via a reference to that type is not necessary. 在任何知道对象是否可转换为类型的情况下,它都很有用,但通过对该类型的引用来获取对象是不必要的。

if ( o is SomeType ) {
  TakeSomeAction();
}

If having a reference of the specified type to the value is useful then it's more efficient to use the 'as' operator instead. 如果指定类型的引用对该值有用,那么使用'as'运算符会更有效。

// Wrong
if ( o is SomeType ) {
  SomeType st = (SomeType)o;
  st.TakeSomeAction();
}

// Right
SomeType st = o as SomeType;
if ( st != null ) {
  st.TakeSomeAction();
}

Actually, I almost never use it. 实际上,我几乎从不使用它。 When I need it, it's usually because I'm going to cast the value to the required type anyway, so I prefer to use as instead. 当我需要它时,通常是因为我要将值转换为所需的类型,所以我更喜欢使用它。

Compare : 相比 :

if (x is MyType)
{
    MyType y = (MyType)x;
    ...
}

And : 而且:

MyType y = x as MyType;
if (y != null)
{
    ...
}

In the first case : 2 operations (1 type check + 1 cast) 在第一种情况下:2次操作(1次检查+ 1次施法)

In the second case : 1 operation (type check + cast in one shot) 在第二种情况下:1次操作(类型检查+一次性投射)

I can't remember ever used is . 我不记得使用过is Most of the time, if you need to know if an instance is of a certain type, you need to use it as such. 大多数情况下,如果您需要知道某个实例是否属于某种类型,则需要使用它。

So I usually directly cast it using as or explicit casting if I'm sure that it actually is of the type in question. 因此,如果我确定它实际上是有问题的类型,我通常会使用as或显式转换直接转换它。

I try to write code that it does not need to cast or find type information more then once. 我尝试编写不需要强制转换或查找类型信息的代码。 So writing is before a cast of a reference type is certainly not recommendable. 所以写作is在引用类型的强制转换之前肯定不值得推荐的。

With the upcoming v4 instead of using is and as there's a different possible approach. 随着即将推出的v4而不是使用is,因为有一种不同的可能方法。

void somemethod(dynamic o)
{
DoStuff(o);
}

DoStuff(neededType o){}
DoStuff(object o){}

that however might have a performnce hit but makes for nice readability 然而,这可能会有一个演奏命中,但可以提供良好的可读性

It depends if you need a reference to the type if it is a type of the given type. 这取决于您是否需要对类型的引用,如果它is给定类型的类型。 If it is then you should use the `as keyworkd. 如果是,那么你应该使用`as keyworkd。

MyObject mo = obj as MyObject;
if (mo == null) {
    // do something.
} else {
    // do something else.
}

which would yield a reference which you can test for null. 这将产生一个你可以测试null的引用。 Otherwise, using the as oprator you'' end up being require to perform a cast anyway. 否则,使用as oprator你最终还是需要执行强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM