Sign in to follow this  
Spa8nky

[C#] Using CopyTo with lists and interfaces. Can't convert to interface type?

Recommended Posts

I have an interface:

[code]

interface IFoo
{
bool IsTasty { get; set; }
}
[/code]

and a class that inherits from it:

[code]

class Bar : IFoo
{
private bool isTasty;

public Bar() { }

public bool IsTasty
{
get { return isTasty; }
set { isTasty = value; }
}
}
[/code]

When I create a list of Bars:

[code]

List<Bar> bars = new List<Bar>();
bars.Add(new Bar());
[/code]

which I wish to store inside an Array of the interface IFoo:

[code]
IFoo[] foos = new IFoo[12];
[/code]

I can do the following:

[code]
foos[0] = bars[0];
[/code]

but if I try and use the 'CopyTo' method, the type conversion fails:

[code]
bars.CopyTo(foos, 0);
[/code]

[quote]

Argument 1: cannot convert from 'Prototype1.IFoo[]' to 'Prototype1.Bar[]'
[/quote]

How can I get this to work correctly?

Share this post


Link to post
Share on other sites
I dare say a C# guru can do it better, but you could do this:

[code]Array.Copy(bars.ToArray<IFoo>(), foos, Math.Min(bars.Count, foos.Length));[/code]

Obviously if you don't mind losing the foo array then you could just overwrite it rather than copy into it...

[code]foos = bars.ToArray<IFoo>();[/code]

Share this post


Link to post
Share on other sites
I thought about using the 'ToArray' method but wasn't sure if it would fare better than iterating over the list and copying them across that way. That way some garbage collection would be avoided.

Share this post


Link to post
Share on other sites
I know it doesn't answer your question, but is there any reason you don't just use an List<IFoo>? Or are you wanting to keep a list of derived types then concatenate them later.?

Share this post


Link to post
Share on other sites
[quote]
[color=#1C2837][size=2]Or are you wanting to keep a list of derived types then concatenate them later.? [/size][/color]
[color=#1C2837][size=2][/quote][/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[size="2"][color="#1c2837"]That's correct. I have a system where all transparent objects are concatenated into an array of IDrawableTransparent(s). I then project their positions onto the camera's forward and sort them by distance so they can be drawn in the correct order.[/color][/size]

Share this post


Link to post
Share on other sites
Ok, so it doesn't work because you're essentially doing ICollection<Bar>.CopyTo( Bar[] array, ...) so you can either have List<IFoo> instead of List<Bar>, convert to a new array as already suggested or you can take the opportunity to have your own sort in place which may(not) turn out to be the best / fast approach for your implimentation.

What you could do with is a CopyFrom() ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this