6

Assuming I have an instance of an object that I know belongs to a subclass of a certain subtype passed to me through a reference of a supertype in C#, I'm used to seeing typecasting done this Java-like way (Assuming "reference" is of the supertype):

if (reference is subtype){
subtype t = (subtype)reference;
}

But recently I've come across examples of what appears to be the same thing done this way:

if (reference is subtype){
subtype t = reference as subtype;
}

Are those two completely equivalent? Is there any difference?

16

7 Answers 7

11

The difference is one will throw an exception and the other one will return a null value if the casting is incorrect. Also, "as" keyword does not work on value type.

BaseType _object;

//throw an exception
AnotherType _casted = (AnotherType) _object; 

//return null
AnotherType _casted = _object as AnotherType;

Edit:

In the example of Fabio de Miranda, the exception will not be thrown due to the use of "is" keyword which prevents to enter in the "if" statement.

5
  • 1
    No, used as stated in the question neither will throw an exception.
    – Guffa
    Jul 22, 2009 at 21:43
  • 2
    Once you understand the difference, you don't really expect "as" to work on a value type, because value types are never null. Jul 22, 2009 at 21:43
  • The only difference in the OP example are performance and the fact the second won't work with value types. The cast will always work. Jul 22, 2009 at 21:50
  • @Guffa: True, but this answers the essence of the question. Incorrect casting with () throws exceptions; incorrect casting with "as" returns null. That's the major difference.
    – Sean
    Jul 22, 2009 at 21:51
  • @Guffa: I agree with you that in his example, the code will not throw an exeception due to the use of "is" keyword. But the question title is "Difference between (subtype)data and data as subtype." I will add some clarification to include your comment in consideration.
    – Francis B.
    Jul 22, 2009 at 21:59
6

They are the same used that way, but neither is the best option. You should do the type checking only once:

subtype t = reference as subtype;
if (t != null) {
   ...
}

Checking for null is more efficient than checking for type.

1
  • 1
    For reference types, this is the way to do it. For value types, you have to use is+(cast). Jul 22, 2009 at 21:40
1

"as" tries to cast reference to subtype & returns null, if it fails.
Explicit casting when fails, throws InvalidCastException.

1

No they are not equivalent. The first will work on any type but the second example will only work on reference types. The reason why is the "as" operator is only valid for a reference type.

2
  • That's halfway correct - as won't work on vanilla value types, but it will work on nullable ones, and it's definitely a point of difference. Jul 22, 2009 at 21:40
  • @Pavel, nullable types are so heavily special cased it's hard to consider them value types anymore :)
    – JaredPar
    Jul 22, 2009 at 22:06
1

http://msdn.microsoft.com/en-us/library/cscsdfbt(VS.71).aspx

which says: "as" equals to:

expression is type ? (type)expression : (type)null;

That means means second one is kind of too much.

1
  • 1
    With expression being evaluated only once! Beware of side-effects! Jul 22, 2009 at 21:44
1

1. As far I know (possbily outdated) "as" is faster then cast.

2. Neither of your examples are optimal. The optimal way is:

var v = reference as subtype;
if (v != null){
  // Do somthing.
}

In this way you do not have double-casting problem.

3
  • Code markup would help you get votes. Just put four spaces before your code. Jul 22, 2009 at 21:43
  • I do not know why, but code want to be formated. I have tried 4 sapaces... Strange. Anywhere my answer is duplicate, so why bother? Jul 22, 2009 at 21:45
  • @Mike - apparently if you start with an ordered list and put code beneath it, it treats it as part of the same line containing the nearest <li> - interesting...
    – John Rasch
    Jul 22, 2009 at 21:57
-1

One important thing about that is that, when using the as operator, such type should be nullable since, should it fail, it will return null. Whereas, using C-style casting, will throw an Exception when the cast can't be performed.

4
  • Well, it has to be because 'as' does not work for value types, and all reference types are nullable. Jul 22, 2009 at 21:41
  • And how come it deserves a -1? I might be stating a fact but a fact is a fact.
    – Anzurio
    Jul 22, 2009 at 21:42
  • Disclaimer: I didn't downvote. You can't simply answer facts for being facts. The tooltip on the down arrow reads: "This answer is not helpful". That's the point. Jul 22, 2009 at 21:48
  • I gave you the -1 because it doesn't make sense. The program won't compile if the type after the 'as' is a value type, so why bother to say that at all? Also, it did not explain the difference between the two before you edited the post. Jul 22, 2009 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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