1

Possible Duplicate:
Casting vs using the 'as' keyword in the CLR
C#: “is” vs “as”

This code:

    if (sheet.Models.Data is GroupDataModel)
    {
        GroupDataModel gdm = (GroupDataModel)sheet.Models.Data;

and this code:

    GroupDataModel gdm = sheet.Models.Data as GroupDataModel;
    if (gdm != null)
    {

Do you recommend any of the two styles above over the other one?

Community
  • 1
  • 1
Bohn
  • 26,091
  • 61
  • 167
  • 254
  • The first. The second may throw an exception if the casting is not possible – Andre Calil Aug 30 '12 at 19:33
  • 2
    Define "better" - for what? Performance? Readability? Errors? Something else? – Oded Aug 30 '12 at 19:34
  • First one has the advantage of not introducing a possibly null symbol into your program – antlersoft Aug 30 '12 at 19:36
  • @AndreCalil - The type interrogation will prevent an InvalidCastException. However, a NullReferenceException *is* still possible in the first option, but not the second. – KeithS Aug 30 '12 at 19:38
  • @KeithS: Both can throw `NullReferenceException` because `sheet` or `sheet.Models` could be null. – Mark Byers Aug 30 '12 at 19:55

5 Answers5

5

The latter is advocated by FxCop, as the cast only need be performed once (the as)

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
3

I think its better to go for as because it convert as well as you can do check easily ..second one is good

More about this: Explicit and Implicit Casting of object and Role of 'is' and 'as' keyword in Explicit casting

Difference between as and is

  • as operator do conversion form one type to another type and return Null if converstion fails. There is no need to conversion again if its convertible.

  • is operator checks weather one object is convertible in another type or not and return false if not. So need to convert object to base type if it convertible.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 2
    The amount of effort needed to perform a cast is infinitesimal. This is certainly a micro-optimization. You should use the code that's more readable, or that otherwise logically makes more sense to you. Performance shouldn't be a concern here. – Servy Aug 30 '12 at 20:04
2

Interestingly, the two versions produce identical IL in this case:

Code:

void Main() {
    Class1 inst1 = new Class1();
    ((inst1 as Class1) != null).Dump();

    Class1 inst2 = new Class1();
    (inst2 is Class1).Dump();
}

class Class1 { }

IL:

IL_0001:  newobj      UserQuery+Class1..ctor
IL_0006:  stloc.0     
IL_0007:  ldloc.0     
IL_0008:  ldnull      
IL_0009:  ceq         
IL_000B:  ldc.i4.0    
IL_000C:  ceq         
IL_000E:  call        LINQPad.Extensions.Dump
IL_0013:  pop         
IL_0014:  newobj      UserQuery+Class1..ctor
IL_0019:  stloc.1     
IL_001A:  ldloc.1     
IL_001B:  ldnull      
IL_001C:  ceq         
IL_001E:  ldc.i4.0    
IL_001F:  ceq         
IL_0021:  call        LINQPad.Extensions.Dump

Class1..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        System.Object..ctor
IL_0006:  ret   
qxn
  • 17,162
  • 3
  • 49
  • 72
  • 2
    `is` is implemented in terms of `as` ( http://blogs.msdn.com/b/ericlippert/archive/2010/09/16/is-is-as-or-is-as-is.aspx ). `((bar as Foo) != null)` is therefore obviously the same as `(bar is Foo)` which is what you're demonstrating – 3Doubloons Aug 30 '12 at 20:03
1

The second version is preferred.

The second version only requires one type test. The first requires two type tests if the test succeeds (first for is and then again for the cast).

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

If you are going to use the typed object afterwards use "as".

If you are not going to use the object with the type itself, use "is".

It is another concern if you EXPECT these objects to be of that particular type. Then just use cast and let it throw exception if type is not what you expect.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
aiodintsov
  • 2,545
  • 15
  • 17