7 Replies - 3640 Views - Last Post: 28 March 2009 - 08:21 AM Rate Topic: -----

#1 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Argument type for a generic List?

Post icon  Posted 26 March 2009 - 05:46 AM

Newbie question. I am trying to create a function that accepts a List (any type). The list contents are not important to the method, just the fact that the argument is some type of list. But for the life of me, I can't figure out the correct type to use. Can anyone point me in the right direction?

ClassDoStuff {

   // What type should be used here to accept either listOfStuff or listOfToys?
   void doStuffWithList(SomeListTypeHere list) 
   {	
		  //... code here 
   }
}

ClassA {
..

	List<Stuff> listOfStuff = new List<Stuff>();
	doStuffWithList(listOfStuff);
}
ClassB {
..

	List<PetToy> listofToys = new List<PetToy>();
	doStuffWithList(listofToys);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Argument type for a generic List?

#2 bodom658   User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Argument type for a generic List?

Posted 26 March 2009 - 06:49 AM

Check this out: http://msdn.microsof...564(vs.80).aspx
Was This Post Helpful? 0
  • +
  • -

#3 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Argument type for a generic List?

Posted 26 March 2009 - 08:20 AM

Thanks for the response. I was actually reading that section before posting. I try to research first and only post questions when I truly cannot figure something out on my own.

But obviously I am missing something simple, because I am still not sure what type to use for the method argument. I have tried everything from IList to IComparable, but they require I provide a type, which is what I am trying to avoid. So it is apparent I am missing a fundamental C# concept...

This post has been edited by sansclue: 26 March 2009 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#4 bodom658   User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Argument type for a generic List?

Posted 26 March 2009 - 08:53 AM

are you talking about a type for List?

like List<what goes in here>?
Was This Post Helpful? 0
  • +
  • -

#5 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Argument type for a generic List?

Posted 26 March 2009 - 09:17 AM

View Postbodom658, on 26 Mar, 2009 - 07:53 AM, said:

are you talking about a type for List?

like List<what goes in here>?



Yep. Like in my example above: List<Stuff> or List<PetToy>. I was trying to figure out how to create a method that would accept any list object, not just List<Stuff>. Is that even possible?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Argument type for a generic List?

Posted 26 March 2009 - 09:53 AM

Have you tried
thelist<IList>


or what about defining an interface(I_AList) and implement it of the classes (stuff) and (Toy). This way the generic case is I_AList.
Was This Post Helpful? 1

#7 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Argument type for a generic List?

Posted 26 March 2009 - 10:43 AM

View PostAdamSpeight2008, on 26 Mar, 2009 - 08:53 AM, said:

Have you tried
thelist<IList>


or what about defining an interface(I_AList) and implement it of the classes (stuff) and (Toy). This way the generic case is I_AList.


Thanks. Yep, that works ... and I think it is the only way (other than extends).

I was confused by the whole covariant/contravariant issue. I thought generics were covariant/contravariant, and that I could use something like IList<Object> would accept both types. But apparently I was wrong. They are not .. and I can't :-)
http://msdn.microsof...mentals_topic12
Was This Post Helpful? 0
  • +
  • -

#8 egof   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: Argument type for a generic List?

Posted 28 March 2009 - 08:21 AM

Have you tried this?
   void doStuffWithList<T>(T list) 
   {	
		  //... code here 
   } 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1