tags:

views:

1734

answers:

13

I'm learning about design patterns and because of that I've ended using a lot of interfaces. One of my "goals" is to program to an interface, not an implementation.

What I've found is that I'm doing a lot of casting or object type conversion. What I'd like to know is if there is a difference between these two methods of conversion:

public interface IMyInterface
{
    void AMethod();
}

public class MyClass : IMyInterface
{
    public void AMethod()
    {
       //Do work
    }

    // other helper methods....
}

public class Implementation
{
    IMyInterface _MyObj;
    MyClass _myCls1;
    MyClass _myCls2;

    public Implementation()
    {
     _MyObj = new MyClass();

     // What is the difference here:
     _myCls1 = (MyClass)_MyObj;
     _myCls2 = (_MyObj as MyClass);
    }
}

If there is a difference, is there a cost difference or how does this affect my program?

Hopefully this makes sense. Sorry for the bad example; it is all I could think of...

Update: What is "in general" the preferred method? (I had a question similar to this posted in the 'answers'. I moved it up here at the suggestion of Michael Haren. Also, I want to thank everyone who's provided insight and perspective on my question.

+15  A: 

"as" will return NULL if not possible to cast.

casting before will raise an exception.

For the performance, raising an exception is usually more costly in time.

Daok
Exception raising is more costly, but if you know that the object can be cast correctly, _as_ requires more time because of the safety check (see Anton's response). However, the cost of the safety check is, I believe, quite small.
Ryan Riley
The cost of potentially raising an exception is a factor to consider, but it is often the correct design.
Jeffrey L Whitledge
@panesofglass - For reference types, the conversion compatibility will always be checked at run-time for both as and cast, so that factor will not distinguish between the two options. (If this were not so, then cast could not raise an exception.)
Jeffrey L Whitledge
@Jeffery - why would that be the "correct design"?
Frank V
@Frank - If you are required to use a pre-generics collection, for example, and a method in your API requires a list of Employees, and some joker instead passes a list of Products, then an invalid cast exception may be appropriate to signal the violation of the interface requirements.
Jeffrey L Whitledge
+3  A: 

If the cast fails, the 'as' keyword doesn't throw an exception; it sets the variable to null (or to its default value for value types) instead.

DannySmurf
No default values for value types. As can not be used for casting value types.
Patrik Hägne
The 'as' keyword doesn't work on value types actually, so it always sets to null.
Erik van Brakel
Good to know, thanks.
DannySmurf
+10  A: 

as never throws an exception if it cannot perform the conversion returning null instead (as operates on reference types only). So using as is basically equivalent to

_myCls2 = _myObj is MyClass ? (MyClass)_myObj : null;

C-style casts, on the other hand, throw an exception when no conversion is possible.

Anton Gogolev
Equivalent, yes, but not the same. This generates way more code then as.
plinth
+1  A: 

The as keyword works the same as an explicit cast between compatible reference types with the major difference that it does not raise an exception if conversion fails. Rather, it yields a null value in the target variable. Since Exceptions are very expensive in terms of performance, it is considered a much better method of casting.

Cerebrus
+6  A: 

Not really an answer to your question, but what I think is an important related point.

If you are programming to an interface you shouldn't be needing to cast. Hopefully these casts are very rare. If not you likely need to rethink some of your interfaces.

Jeremy Wilde
The casting, so far, has mostly been needed for my Unit Testing but thank you for bringing it up. I'll keep that in my mind while I work on this.
Frank V
+1 Was going to bring that up
johnc
+4  A: 

The as operator can only be used on reference types, it cannot be overloaded, and it will return null if the operation fails. It will never throw an exception.

Casting can be used on any compatible types, it can be overloaded, and it will throw an exception if the operation fails.

The choice of which to use depends on the circumstances. Primarily, it's a matter of whether you want to throw an exception on a failed conversion.

Jeffrey L Whitledge
'as' can also be used on nullable value types, which provides an interesting pattern. See my answer for code.
Jon Skeet
I've been bested by the Skeet again! Next time! Next time!!!! :)
Jeffrey L Whitledge
+1  A: 

It depends, do you want to check for null after using "as" or would you prefer your app to throw an exception?

My rule of thumb is if I always expect the variable to be of the type I am expecting at the time I want I use a cast. If it is possible that the variable will not cast to what I want and I am prepared to handle nulls from using as, I will use as.

Darryl Braaten
+61  A: 

I don't think any of the answers so far (at the time of starting this answer!) have really explained where it's worth using which.

  • Don't do this:

    // Bad code - checks type twice for no reason
    if (randomObject is TargetType)
    {
        TargetType foo = (TargetType) randomObject;
        // Do something with foo
    }
    

    Not only is this checking twice, but it may be checking different things, if randomObject is a field rather than a local variable. It's possible for the "if" to pass but then the cast to fail, if another thread changes the value of randomObject between the two.

  • If randomObject really should be an instance of TargetType, i.e. if it's not, that means there's a bug, then casting is the right solution. That throws an exception immediately, which means that no more work is done under incorrect assumptions, and the exception correctly shows the type of bug.

    // This will throw an exception if randomObject is non-null and
    // refers to an object of an incompatible type. The cast is
    // the best code if that's the behaviour you want.
    TargetType convertedRandomObject = (TargetType) randomObject;
    
  • If randomObject might be an instance of TargetType and TargetType is a reference type, then use code like this:

    TargetType convertedRandomObject = randomObject as TargetType;
    if (convertedRandomObject != null)
    {
        // Do stuff with convertedRandomObject
    }
    
  • If randomObject might be an instance of TargetType and TargetType is a value type, then we can't use as with TargetType itself, but we can use a nullable type:

    TargetType? convertedRandomObject = randomObject as TargetType?;
    if (convertedRandomObject != null)
    {
        // Do stuff with convertedRandomObject.Value
    }
    

    (Note: currently this is actually slower than is + cast. I think it's more elegant and consistent, but there we go.)

  • If you really don't need the converted value, but you just need to know whether it is an instance of TargetType, then the is operator is your friend. In this case it doesn't matter whether TargetType is a reference type or a value type.

  • There may be other cases involving generics where is is useful (because you may not know whether T is a reference type or not, so you can't use as) but they're relatively obscure.

  • I've almost certainly used is for the value type case before now, not having thought of using a nullable type and as together :)


EDIT: Note that none of the above talks about performance, other than the value type case, where I've noted that unboxing to a nullable value type is actually slower - but consistent.

As per naasking's answer, is-and-cast or is-and-as are both as fast as as-and-null-check with modern JITs, as shown by the code below:

using System;
using System.Diagnostics;
using System.Linq;

class Test
{
    const int Size = 30000000;

    static void Main()
    {
        object[] values = new object[Size];
        for (int i = 0; i < Size - 2; i += 3)
        {
            values[i] = null;
            values[i + 1] = "x";
            values[i + 2] = new object();
        }
        FindLengthWithIsAndCast(values);
        FindLengthWithIsAndAs(values);
        FindLengthWithAsAndNullCheck(values);
    }

    static void FindLengthWithIsAndCast(object[] values)        
    {
        Stopwatch sw = Stopwatch.StartNew();
        int len = 0;
        foreach (object o in values)
        {
            if (o is string)
            {
                string a = (string) o;
                len += a.Length;
            }
        }
        sw.Stop();
        Console.WriteLine("Is and Cast: {0} : {1}", len,
                          (long)sw.ElapsedMilliseconds);
    }

    static void FindLengthWithIsAndAs(object[] values)        
    {
        Stopwatch sw = Stopwatch.StartNew();
        int len = 0;
        foreach (object o in values)
        {
            if (o is string)
            {
                string a = o as string;
                len += a.Length;
            }
        }
        sw.Stop();
        Console.WriteLine("Is and As: {0} : {1}", len,
                          (long)sw.ElapsedMilliseconds);
    }

    static void FindLengthWithAsAndNullCheck(object[] values)        
    {
        Stopwatch sw = Stopwatch.StartNew();
        int len = 0;
        foreach (object o in values)
        {
            string a = o as string;
            if (a != null)
            {
                len += a.Length;
            }
        }
        sw.Stop();
        Console.WriteLine("As and null check: {0} : {1}", len,
                          (long)sw.ElapsedMilliseconds);
    }
}

On my laptop, these all execute in about 60ms. Two things to note:

  • There's no significant difference between them. (In fact, there are situations in which the as-plus-null-check definitely is slower. The above code actually makes the type check easy because it's for a sealed class; if you're checking for an interface, the balance tips slightly in favour of as-plus-null-check.)
  • They're all insanely fast. This simply will not be the bottleneck in your code unless you really aren't going to do anything with the values afterwards.

So let's not worry about the performance. Let's worry about correctness and consistency.

I maintain that is-and-cast (or is-and-as) are both unsafe when dealing with variables, as the type of the value it refers to may change due to another thread between the test and the cast. That would be a pretty rare situation - but I'd rather have a convention which I can use consistently.

I also maintain that the as-then-null-check gives a better separation of concerns. We have one statement which attempts a conversion, and then one statement which uses the result. The is-and-cast or is-and-as performs a test and then another attempt to convert the value.

To put it another way, would anyone ever write:

int value;
if (int.TryParse(text, value))
{
    value = int.Parse(text);
    // Use value
}

That's sort of what is-and-cast is doing - although obviously in a rather cheaper way.

Jon Skeet
so you are mentioning that "is" operator is wrong in this situation, but when is ok to use "is". Thanks
Oscar Cabrero
Editing answer to explain that.
Jon Skeet
Here is the cost of is/as/casting in terms of IL: http://www.atalasoft.com/cs/blogs/stevehawley/archive/2009/01/30/is-as-and-casting.aspx
plinth
"If randomObject really should be an instance of TargetType" - can't you use as, check for null, and in the else statement throw the exception? As (no pun intended) that avoids the double cast
Chris S
@Chris: Yes, you can - but why not just cast instead? There's no double cast then, and it's a single line of code. Will add example in the answer.
Jon Skeet
@Jon Skeet: Didn't know that `as ValueType?` works! Very interesting! +1
IgorK
int? y = 7;int i = y as int?; Does not compile: Cannot implicitly convert type 'int?' to 'int'.
nils_gate
Oops - that should be TargetType? convertedRandomObject. Will fix.
Jon Skeet
Jay Bazuzi
+1: should be a page in my C# book
JYelton
In case, if targetObject _might_ be target type, why using of "is" and cast combination considered a bad practice? I mean,it generates a slower code, but in this case intentions are more clear than AS cast, like "Do something if targetObject is targetType", instead of "Do something if targetObject is null", Moreover AS clause will create a unnecessary variable out of IF scope.
Valera Kolupaev
@Valera: Good points, although I'd suggest that the as/null test is sufficiently idiomatic that the intention should be clear to almost all C# developers. I don't like the duplication involved in the is + cast, personally. I'd actually like a sort of "as-if" construct which does both actions in one. They go together so often...
Jon Skeet
Jon - any opinion on naasking's answer below?
Richard Watson
on my Machine:Is And Cast: 1208, Is and As:1239, As And null Check: 1279
Behrooz
@Behrooz: Try it with an interface instead of string - I'd be interested to hear your results. (Also run it several times - I've found the results can vary quite a bit.) In general, anything you can do 30 million times in just over a second is unlikely to be a bottleneck :)
Jon Skeet
@Jon Skeet:sorry for my late.Is And Cast:2135, Is And As:2145, As And null check: 1961,specs: OS:Windows Seven, CPU:i5-520M, 4GB of DDR3 1033 ram, benchmark on array of 128,000,000 items.
Behrooz
+2  A: 

This is not an answer to the question but comment to the question's code example:

Usually you should not have to cast an Object from e.g. IMyInterface to MyClass. The great thing about interfaces is that if you take an object as input that implements an interface, than you don't have to care what kind of object you are getting.

If you cast IMyInterface to MyClass, than you already assume that you get an object of type MyClass and it makes no sense to use IMyInterface, because if you feed your code with other classes that implement IMyInterface, it would break your code...

Now, my advice: if your interfaces are well designed you can avoid a lot of typecasting.

f3lix
+6  A: 

One of the more subtle differences between the two is that the "as" keyword can not be used for casting when a cast operator is involved:

public class Foo
{
    public string Value;

    public static explicit operator string(Foo f)
    {
        return f.Value;
    }

}

public class Example
{
    public void Convert()
    {
        var f = new Foo();
        f.Value = "abc";

        string cast = (string)f;
        string tryCast = f as string;
    }
}

This will not compile (although I think it did in previous versions) on the last line since the "as" keywords do not take cast operators into account. The line string cast = (string)f; works just fine though.

Patrik Hägne
Excellent. I was looking for a comment that pointed out this difference.
Neil Whitaker
A: 

What you choose strongly depends on what required. I prefer explicit casting

IMyInterface = (IMyInterface)someobj;

because if object should by of IMyInterface type and it is not - it is definitely problem. It is better to get error as early as possible because exact error will be fixed instead of fixing its side effect.

But if you deal with methods that accepts object as parameter then you need to check its exact type prior executing any code. In such case as would be useful so you can avoid InvalidCastException.

Oleg
+7  A: 

Here's another answer, with some IL comparison. Consider the class:

public class MyClass
{
    public static void Main()
    {
     // Call the 2 methods
    }

    public void DirectCast(Object obj)
    {
     if ( obj is MyClass)
     { 
      MyClass myclass = (MyClass) obj; 
      Console.WriteLine(obj);
     } 
    } 


    public void UsesAs(object obj) 
    { 
     MyClass myclass = obj as MyClass; 
     if (myclass != null) 
     { 
      Console.WriteLine(obj);
     } 
    }
}

Now look at the IL each method produces. Even if the op codes mean nothing to you, you can see one major difference - isinst is being called followed by castclass in the DirectCast method. So two calls instead of one basically.

.method public hidebysig instance void  DirectCast(object obj) cil managed
{
  // Code size       22 (0x16)
  .maxstack  8
  IL_0000:  ldarg.1
  IL_0001:  isinst     MyClass
  IL_0006:  brfalse.s  IL_0015
  IL_0008:  ldarg.1
  IL_0009:  castclass  MyClass
  IL_000e:  pop
  IL_000f:  ldarg.1
  IL_0010:  call       void [mscorlib]System.Console::WriteLine(object)
  IL_0015:  ret
} // end of method MyClass::DirectCast

.method public hidebysig instance void  UsesAs(object obj) cil managed
{
  // Code size       17 (0x11)
  .maxstack  1
  .locals init (class MyClass V_0)
  IL_0000:  ldarg.1
  IL_0001:  isinst     MyClass
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brfalse.s  IL_0010
  IL_000a:  ldarg.1
  IL_000b:  call       void [mscorlib]System.Console::WriteLine(object)
  IL_0010:  ret
} // end of method MyClass::UsesAs

The isinst keyword versus the castclass

This blog post has a decent comparison between the two ways of doing it. His summary is:

  • In a direct comparison, isinst is quicker than castclass (although only slightly)
  • When having to perform checks to ensure the conversion was successful, isinst was significantly quicker than castclass
  • A combination of isinst and castclass should not be used as this was far slower than the quickest "safe" conversion (over 12% slower)

I personally always use As, because it's easy to read and is recommended by the .NET development team (or Jeffrey Richter anyway)

Chris S
+2  A: 

Please ignore Jon Skeet's advice, re:avoid test-and-cast pattern, ie.:

if (randomObject is TargetType)
{
    TargetType foo = randomObject as TargetType;
    // Do something with foo
}

The idea that this costs more than a cast and a null test is a MYTH:

TargetType convertedRandomObject = randomObject as TargetType;
if (convertedRandomObject != null)
{
    // Do stuff with convertedRandomObject
}

It's a micro-optimization that does not work. I ran some real tests, and test-and-cast is actually faster than cast-and-null-comparison, and it's safer too because you don't have the possibility of having a null reference in the scope outside the if should the cast fail.

If you want a reason why test-and-cast is faster, or at least not slower, there's a simple and complex reason.

Simple: even naive compilers will coalesce two similar operations, like test-and-cast, into a single test and branch. cast-and-null-test may force two tests and a branch, one for the type test and conversion to null on failure, one for the null check itself. At the very least, they will both optimize to a single test and branch, so test-and-cast would be neither slower nor faster than cast-and-null-test.

Complex: why test-and cast is faster: cast-and-null-test introduces another variable into the outer scope which the compiler must track for liveness, and it may not be able to optimize away that variable depending on how complex your control-flow is. Conversely, test-and-cast introduces a new variable only in a delimited scope so the compiler knows that the variable is dead after the scope exits, and so can optimize register allocation better.

So please, PLEASE let this "cast-and-null-test is better than test-and-cast" advice DIE. PLEASE. test-and-cast is both safer and faster.

naasking
I appreciate your controversial, detailed answer. I'm not willing to switch the "answer" to this question simply on the basis of your explanation (even though, I see your point.) Fact is, Skeet's answer is the popular answer (along with making technical sense to me.) I'm certainly not saying that you aren't on to something but I'd like to see further support. Perhaps others have completed similar research. If you can find it, post it.
Frank V
Well, the source code is available from my tests, so you can run them yourself if you like. I'd be interested to hear your results.
naasking
@naasking: If you test twice (as per your first snippet), there's a chance that the type will change between the two tests, if it's a field or `ref` parameter. It's safe for local variables, but not for fields. I would be interested to run your benchmarks, but the code you've given in your blog post is not complete. I agree with not micro-optimising, but I don't think using the value twice is any more readable or elegant than using "as" and a nullity test. (I would definitely use a straight cast rather than "as" after an is, btw.)
Jon Skeet
I also don't see why it's any safer. I've shown why it's *less* safe, in fact. Sure, you end up with a variable in scope which may be null, but unless you start using that outside the scope of the subsequent "if" block, you're fine. The safety concern I've raised (around fields changing their value) is a genuine concern *with the code shown* - your safety concern requires developers to be lax in other code.
Jon Skeet
+1 for pointing out that is/cast or as/cast is no slower in reality, mind you. Having run a complete test myself, I can confirm it makes no difference as far as I can see - and frankly you can run a *boggling* number of casts in a very small time. Will update my answer with full code.
Jon Skeet
Indeed, if the binding is not a local there is a chance of a TOCTTOU bug (time-of-check-to-time-of-use), so good point there. As for why it's safer, I work with a lot of junior developers that like to reuse locals for some reason. cast-and-null is thus very real hazard in my experience, and I've never run into a TOCTTOU situation since I don't design my code that way. As for runtime test speed, it's even faster than virtual dispatch [1]! Re: code, I'll see if I can find the source for the cast test.[1] http://higherlogics.blogspot.com/2008/10/vtable-dispatching-vs-runtime-tests-and.html
naasking
@naasking: I've never run into the local reuse problem - but I'd say it's easier to spot in code review than the more subtle TOCTTOU bug. It's also worth pointing out that I've just rerun my own benchmark checking for interfaces instead of a sealed class, and that tips the performance in favour of as-then-null-check... but as I've said, performance isn't why I'd choose any particular approach here.
Jon Skeet