List<> issue

Hi,

Whats wrong with that code (in Beta 2) ?

List<Article> articles =newList<Article>();
articles.Add(article);
articles.Add(article2);

DataStore.Save(articles);

DataStore.cs
publicstaticvoid Save(IEnumerable<IPersistable> persistables)

Article.cs
publicpartialclassArticle :Content,IPersistable

As far as I know:
publicclassList<T> :IList<T>,ICollection<T>,IEnumerable<T>,IList,ICollection,IEnumerable

At compile time I get this error on the line DataStore.Save:
Error 9 The best overloaded method match for 'TechHeadBrothers.Portal.DAL.DataStore.Save(System.Collections.Generic.IEnumerable<IPersistable>)' has some invalid arguments
Error 4 Argument '1': cannot convert from 'System.Collections.Generic.List<TechHeadBrothers.Portal.Entities.Article>' to 'IPersistable'

Regards,
Laurent Kemp

[2535 byte] By [LaurentKemp] at [2007-12-16]
# 1

Your list implements IEnumerable<Article>, not IEnumerable<IPersistable>. They are not the same, and since one does not derive from the other, there is no conversion. Try...

DataStore.cs
public static void Save<T>(IEnumerable<T> persistables) where T : IPersistable

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Article implements IPersistable so it should work, no ? I also tried you proposition.

Regards,
Laurent Kemp

LaurentKemp at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3
No it should not work. IEnumerable<Article> is not an IEnumerable<IPersistable>. I can't think of a good way to illustrate this using IEnumerable<T>, but I can illustrate it using IList<T>. According to your logic, the following should be valid.

given...


public class SomethingElse : IPersistable

try this...


List<Article> articles = new List<Article>;
IList<IPersistable> listInterface = (IList<IPersistable>) articles;
listInterface.Add( new SomethingElse());

Clearly the cast should not work, because if it did I would have added a SomethingElse to the list of Articles.

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...