views:

368

answers:

7

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?

A: 

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.

Anzurio
Well, it has to be because 'as' does not work for value types, and all reference types are nullable.
Ed Swangren
And how come it deserves a -1? I might be stating a fact but a fact is a fact.
Anzurio
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.
Martinho Fernandes
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.
Ed Swangren
meh, rubbish. .
Anzurio
+1  A: 

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

shahkalpesh
+11  A: 

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.

Francis B.
No, used as stated in the question neither will throw an exception.
Guffa
Once you understand the difference, you don't really expect "as" to work on a value type, because value types are never null.
Joel Coehoorn
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.
Martinho Fernandes
@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
@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.
+1  A: 

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.

JaredPar
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.
Pavel Minaev
@Pavel, nullable types are so heavily special cased it's hard to consider them value types anymore :)
JaredPar
+3  A: 

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.

Guffa
For reference types, this is *the* way to do it. For value types, you have to use `is`+(cast).
280Z28
+1  A: 

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.

Mike Chaliy
Code markup would help you get votes. Just put four spaces before your code.
Martinho Fernandes
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?
Mike Chaliy
@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
+1  A: 

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.

Maxim Alexeyev
With expression being evaluated only once! Beware of side-effects!
Martinho Fernandes