views:

1141

answers:

9

Hi

Why can I not cast a List<ObjBase> as List<Obj>? Why does the following not work:

internal class ObjBase
   {
   }

internal class Obj : ObjBase
   {
   }   

internal class ObjManager
{
    internal List<Obj> returnStuff()
    {
       return getSomeStuff() as List<Obj>;
    }

    private List<ObjBase> getSomeStuff()
    {
       return new List<ObjBase>();
    }

}

Instead I have to do this:

internal class ObjBase
   {
   }

internal class Obj : ObjBase
   {
   }

internal class ObjManager
{
    internal List<Obj> returnStuff()
    {
       List<ObjBase> returnedList = getSomeStuff();
       List<Obj> listToReturn = new List<Obj>(returnedList.Count);
       foreach (ObjBase currentBaseObject in returnedList)
       {
          listToReturn.Add(currentBaseObject as Obj);
       }
       return listToReturn;
    }

    private List<ObjBase> getSomeStuff()
    {
       return new List<ObjBase>();
    }
}

I get the following error in Visual Studio 2008 (shortened for readability):

Cannot convert type 'List' to 'List' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Thanks.

+1  A: 

I think you are misunderstanding the cast you are trying to do. You are thinking that you are changing the type of the object that is stored in the list, where you are actually trying to change the type of the list itself. It rather makes sense that you can't change the list itself as you have already populated it.

You might look at it as a list of a base class and then cast it when you are processing the list items, that would be my approach.

What is the purpose of this attempted cast?

Lazarus
+3  A: 

Please look at the following questions:

http://stackoverflow.com/questions/674715/net-casting-generic-list

http://stackoverflow.com/questions/1133356/why-does-this-generic-cast-fail/1133389

gjutras
And http://stackoverflow.com/questions/1263489/how-do-i-upcast-a-collection-of-base-class-in-c/1263514#1263514
Ruben Bartelink
A: 

Linq has a ConvertAll method. so something like

list.ConvertAll<Obj>(objBase => objbase.ConvertTo(obj));

I'm not sure what else to suggest. I assume ObjBase is the base class, and if all ObjBase objects are Obj objects, i'm not sure why you would have the two objects in the first place. Perhaps i'm off the mark.

Edit: the list.Cast method would work better than the above, assuming they are castable to each other. Forgot about that until I read the other answers.

Jimmeh
+2  A: 

Covariance my friend. Look at http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance

Dewfy
+3  A: 

I can only describe the "problem" from a Java view, but from what little I know this aspect is the same in both C# and Java:

A List<ObjBase> is not a List<Obj>, because it could contain an ObjBase object which is not a Obj object.

The other way around a List<Obj> can not be cast to a List<ObjBase> because the former guarantees to accept an Add() call with a ObjBase argument which the former will not accept!

So to summarize: even though a Obj is-a ObjBase a List<Obj> is not a List<ObjBase>.

Joachim Sauer
+1  A: 

C# currently does not support variance for generic types. From what I've read, this will change in 4.0.

See here for more information on variance in generics.

Scott Ivey
+5  A: 

You can use Cast and ToList extension methods from System.Linq to have this in one line.

Instead of

internal List<Obj> returnStuff()
{
   return getSomeStuff() as List<Obj>;
}

do this:

internal List<Obj> returnStuff()
{
   return getSomeStuff().Cast<Obj>().ToList();
}
RaYell
A: 

This is a major pain in C# - this is how generics were designed. List doesn't extend List, its just a completely different type. You can't cast or assign them to each other in any way, your only option is to copy one list to the other one.

Grzenio
A: 

Lazarus:
I thought that the compiler would realise that I wanted actions done on the objects of the list and not that I was trying to cast the list itself.

Some more information:

public abstract class ObjBase
   {
   }

internal interface IDatabaseObject
   {
   }

public class Obj : ObjBase, IDatabaseObject
   {
   }


internal interface IDatabaseObjectManager
   {
      List<ObjBase> getSomeStuff();
   }

public class ObjManager : IObjManager
{
    public List<Obj> returnStuff()
    {
       return getSomeStuff().Cast <Customer>().ToList<Customer>();
    }

    private List<ObjBase> getSomeStuff()
    {
       return new List<ObjBase>();
    }
}

Now client code outside of this DLL can go: ObjManager objM = new ObjManager(); List listOB = objM.returnStuff(); I'm going to be creating several Obj and ObjManager types for this part (O/RM) of the application.

(Darn comment block ran out of characters! :-)

AndrewJacksonZA