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?

5
  • The first. The second may throw an exception if the casting is not possible Aug 30, 2012 at 19:33
  • 2
    Define "better" - for what? Performance? Readability? Errors? Something else?
    – Oded
    Aug 30, 2012 at 19:34
  • First one has the advantage of not introducing a possibly null symbol into your program
    – antlersoft
    Aug 30, 2012 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, 2012 at 19:38
  • @KeithS: Both can throw NullReferenceException because sheet or sheet.Models could be null.
    – Mark Byers
    Aug 30, 2012 at 19:55

5 Answers 5

5

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

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.

1
  • 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, 2012 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   
1
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).

4
  • If the casting is not possible, won't it throw an exception? Aug 30, 2012 at 19:35
  • @AndreCalil: Neither version throws an exception.
    – Mark Byers
    Aug 30, 2012 at 19:35
  • 1
    The first one could, depending on context. In a multithreaded situation sheet.Models.Data might be convertible to GroupDataModel at the time of the type check, but not at the time of the cast.
    – Sean U
    Aug 30, 2012 at 19:48
  • @SeanU: +1 That's a good point. The technical term to describe this is a "race condition".
    – Mark Byers
    Aug 30, 2012 at 19:50
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.