C#: Put member variables into a list.

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-05-21T13:51:45Z Indexed on 2010/05/21 14:00 UTC
Read the original article Hit count: 349

Filed under:
|

Hi all

Assuming I have a method which accepts an IList or similar thing as a parameter:

public void JiggleMyList(IList<string> list)...

Is there an easy way that I can pass in string members of a list of objects?

I mean, if for example, I have a list of Person objects which expose a string property called FullName, is there a quick way to stuff the FullNames of all the Person objects into the method parameter, or do I have to create a new List and iterate through the Person objects:

List<string> fullNames = new List<string>;
foreach (Person person in people)
{
  fullNames.Add(person.FullName);
}
JiggleMyList(fullNames);

I come across this all the time, and it would be nice if there was a shortcut.

Many thanks

David

© Stack Overflow or respective owner

Related posts about c#

Related posts about list