469,648 Members | 1,153 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Convert from List<A> to List<B>

Hi,

Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList) where Destination : Source
{
List<Destination> result = new List<Destination>();
foreach (Source aSource in sourceList)
{
result.Add(aSource as Destination);
}
return result;
}

It didn't work, is it possible?

Why isn't there a method like this:
List<B> aList = new List<B>();
List<A> newList = aList.Convert<A>();

Any ideas?

Thanks,

Jun 20 '06 #1
4 16653
Like so?

List<B> aList = new List<B>();
List<A> newList = aList.ConvertAll<A>(delegate(B item) {return (A) (object)
item;});

The (object) cast is needed where an explicit cast operator is not provided
by the class - or you could build the replacement objects yourself.

Marc
Jun 20 '06 #2
Additional: I didn't spot the "B subclasses A"; this means you can drop the
(object) cast

Finally - is this so you can can pass a List<A> as method parameters? If so,
note that this is a covariance issue, and can be resolved through generics
(see Right() below)

Marc

static void Main() {
List<B> aList = new List<B>();
List<A> newList = aList.ConvertAll<A>(delegate(B item) { return
(A)item; });

Wrong(aList);
Right(aList);
}
public static void Wrong(List<A> items) {
//do stuff with each A
}
public static void Right<T>(List<T> items) where T : A{
//do stuff with each T, which can also be treated as A
}
public class A { }
public class B : A { }
Jun 20 '06 #3
That looks OK. How was it failing?

bo**********@hotmail.com wrote:
Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:
[snip]
It didn't work, is it possible?


Jun 20 '06 #4
<bo**********@hotmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
Hi,

Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList) where Destination : Source
{
List<Destination> result = new List<Destination>();
foreach (Source aSource in sourceList)
{
result.Add(aSource as Destination);
}
return result;
}

It didn't work, is it possible?


If you reverse the where constraint, this works:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList)
where Source : Destination
{
List<Destination> result = new List<Destination>();

foreach (Source aSource in sourceList)
result.Add(aSource);

return result;
}

///ark
Jun 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Michael | last post: by
1 post views Thread by Alex Nitulescu | last post: by
4 posts views Thread by David Lozzi | last post: by
5 posts views Thread by David Longnecker | last post: by
1 post views Thread by shapper | last post: by
2 posts views Thread by Shahid | last post: by
2 posts views Thread by Berryl Hesh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.