2

Possible Duplicate: casting vs using the 'as' keyword in the CLR

As someone new to C# I was wondering if there is any important difference between this:

object o = SomeFunction();
if (o is MyClass)
{
    MyClass myObject = (MyClass) o;
    myObject.MyFunction();
}

and this:

object o = SomeFunction();
MyClass myObject = o as MyClass;
if (myObject != null)
    myObject.MyFunction();

When is one preferred over the other? In the code I work with, both seem to be used randomly.

Community
  • 1
  • 1
Bart Gijssens
  • 1,572
  • 5
  • 20
  • 38
  • 2
    imho it's not a duplicate. He is not asking if regular casting or `as` should be used. He is asking ***when*** to use `as` and when to use `is`. – jgauffin May 27 '11 at 11:42
  • 1
    agreed - duplicate - the question is different, but Jon's answer to the other question compeletely answers this one – BonyT May 27 '11 at 11:45
  • true. did only read the other question. – jgauffin May 27 '11 at 11:47

6 Answers6

0

The second sample won't work for value types as as operator would return null if casting fails. In C#, 'is' operator return true if object's type matches with your provided type else returns false.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38
0

The is operator checks whether an object is compatible with a given type, and the result of the evaluation is a Boolean: true or false. The is operator will never throw an exception.

The as operator works just like casting except the as operator will never throw an exception. Instead, if the object can’t be cast, the result is null. Regular cast '(Type)' will throw an exception if failed. Note that casting null using 'as' produces null. Hence, a null result from an 'as'-cast doesn't always mean that an object isn't of a given type, it could be that the instance is null.

larsmoa
  • 12,604
  • 8
  • 62
  • 85
0

As per your code, if 'o' needs to be used for some operation, then "as" is better. If 'o' is used just to check the type then 'is' is better.

Harish
  • 98
  • 4
0

You should use what gives the most readable code. So it really depends on what you are doing.

IMHO, the is keyword is evil. It invites you to break the Liskov substitution principle.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    I don't see how that's the case. If you have a collection of objects of a given supertype and you want to filter them based on subtype, how is that even related to the Liskov Substitution Principle? – BonyT May 27 '11 at 16:45
  • LSP says that you should always be able to use the base type. The `is` keyword makes it possible to use different logic for different derived types. Methods that are implemented like that wont probably not work with the base type or all derived types (unless the method is updated with code supporting the new base types). It's better to refactor the code to avoid using `is`. – jgauffin Jun 30 '11 at 07:11
  • hmm - good point - I hadn't considered it in those terms - definitely a recipe for clunky code. – BonyT Jun 30 '11 at 08:52
  • @jgauffin Do you think that using overloaded polymorphic functions, could be a general recipe for avoiding the use of "is"? – Snoop Aug 02 '16 at 17:39
  • no. no problem is on another level. you pass a base class where you should have passed a specific type or interface. The problem is rather in how you have designed the invocation chain. It's much easier to answer a question for a specific problem to show how it should have been done instead. – jgauffin Aug 02 '16 at 18:39
0

I think both code do the same thing. I would probably use the first if I wasn't sure of the object am dealing with (for example, dynamically loading a class that's supposed to implement an interface). But will use the second if I am pretty sure (for example, finding a control on a page).

Tundey
  • 2,926
  • 1
  • 23
  • 27
-2

using "as" and checking for null is faster than using "is" and then casting.

Oleg
  • 798
  • 3
  • 7
  • 1
    Are there any measurements you (or somebody else) did or is this just speculation? – Simon Woker May 27 '11 at 11:43
  • 1
    I think I've read somewhere that the "is" operator does a cast beyond the scenes. So basically if you have to cast manually once again to retrieve the object of that type, you have 2 casts instead of one. So, It talks about performance for itself. However, if you only need to know if it is of the type specified and you don't need the object cast - I'd go for the "is" because of readability. In that case you have only one cast, so it should run approx. the same in terms of performances. – Kornelije Petak May 27 '11 at 11:51
  • 1
    Is it? Do you have any reference for that? – Bart Gijssens May 27 '11 at 11:51
  • I guess kornelijepetak did already answer – Oleg May 27 '11 at 12:31