   Passing a ListmyClass as ListObject - C#
  I have a list of a class I made myself.I have a function which I've coded to accept List<Object>.  This function calls the ToString() method of each member of the list to save to file.What I had hoped would happen is that since all classes are Objects and have a ToString() method, the function would accept pretty much any list and get things done.Sadly this does not appear to be the case, even with my attempted typecasting:  foo ( (List<Object>)myList);Can anyone tell me what I'm doing wrong and perhaps suggest a solution?Thanks in advance.   
  What "type" is myList? if its of type list<Object> then it should work fine. If its not (such as a list of a child Object) then it wont' work.Your options are to make it a list of Objects (instead of any child classes) or you could mess with some generics and make the method accept any fitting object. I've never really tried generics (but am quite familiar with c++ templates) so I can't advise any further but I beleive that should work.   ;  A List<Child> is not a List<Base>, even if Child is a subclass of Base. Thus, F(List<object> f) will not accept a List<SomethingElse> as a parameter.What you need here are generics, possibly: F<T>( List<T> f) will accept a List<> of anything.   ;  myList is a List<myCustomClass>.  I was hoping the save function would be able to take pretty much any list, since it's only using the ToString function.Making the list (and other subsequent lists) List<Object> would kick up a bunch of problems I'd have to work back through and solve.  With C# having been pretty sensible and helpful to date I have to believe there's a nice elegant way to get this done.   ;Quote:Making the list (and other subsequent lists) List<Object> would kick up a bunch of problems I'd have to work back through and solve. As I said, that will not actually work. A List<Child> is not a List<Base>. This is true in almost all languages that have inheritance, including C# and C++.Quote:With C# having been pretty sensible and helpful to date I have to believe there's a nice elegant way to get this done.Generics, as in my second example.   ;  Thanks Josh - digging up generics tutorials  :D   ;  Might you be able to suggest what I'm doing wrong having received this error?Error1The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)This particular error is generated in a line of code which is trying to instantiate my generic class like so:Saver<T> saver = new Saver<T>(savepath);Saver itself is defined in the same namespace as:public class Saver<T> And I have the line using System.Collections.Generic at the top of the file.;Quote:Original post by technorabblemyList is a List<myCustomClass>.  I was hoping the save function would be able to take pretty much any list, since it's only using the ToString function.Making a function that takes a List is bad form anyway. For the purpose described here, either require IList, or even IEnumerable or ICollection.Generic functions should prefer to work with interfaces.   ;Quote:Original post by technorabbleMight you be able to suggest what I'm doing wrong having received this error?Error1The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)This particular error is generated in a line of code which is trying to instantiate my generic class like so:Saver<T> saver = new Saver<T>(savepath);Saver itself is defined in the same namespace as:public class Saver<T> And I have the line using System.Collections.Generic at the top of the file."T" isn't a type. You can use "T" as the placeholder for the type in the definition of a generic class, but an actual generic class instance must be provided a real type. Replace T in the line where you are creating a new instance with a real type. Try int, for example.   ;  There are actually a few ways to solve this problem.  The first, creating a generic class, has already been suggested, and is probably the best solution.You could also use a generic method, instead of a generic class, like so (there could be a mistake in this, as I'm not at home, but you should get the idea)public class Saver{  public Saver(string path)  {    ...  }  public void Save<T>(IEnumerable<T> items)  {    foreach (T item in items)    { string toStrign = item.ToString(); //Do whatever here    }  }}public class Test{  public void TestStuff(List<int> myList)  {new Saver("test").Save(myList)  }}Note that when calling the generic method this way, you don't even need to specify the type explicitly (i.e. new Saver("test").Save<int>(myList);).  The language can infer the type usage from the parameter you passed.Another way to solve this problem is to use LINQ's built in IEnumerable<T> extension methods, if you are programming in a recent version of .NET, namely Cast<T>.  This method will essentially convert an IEnumerable<A> to an IEnumerable<B> by casting each member.[Edited by - TheOrzo on April 7, 2010 9:04:48 AM]