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,IPersistableAs far as I know:
publicclassList<T> :IList<T>,ICollection<T>,IEnumerable<T>,IList,ICollection,IEnumerableAt 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
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
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.