Converter and ConvertAll()

Ok, So Mitch’s blog is definitely getting added to my blog roll.  Building on my previous example let’s assume we want to get a list of all the ages of our customers and work with them as a list of int’s rather then dealing with them as a property of the customer, enter Converter<T>.

List<int> allAges = customers.ConvertAll<int>(delegate(Customer customer)
{
   return customer.Age;
}
);

Assert.AreEqual(customers.Count, allAges.Count);
allAges.ForEach(delegate(int i)
{
   Console.WriteLine(i);
}
);

This allows us to convert all objects in one list to another list. Cool!

# re: Converter<T> and ConvertAll()

Sunday, April 24, 2005 5:41 PM by Frans Bouma    
I must say I find this code unreadable. I had to read it over and over again before I got it. I'd prefer a few more lines which are a bit more clear. BUt perhaps it's a lack of generics experience at the moment..

# re: Converter<T> and ConvertAll()

Sunday, April 24, 2005 10:52 PM by Steve    
I agree that it takes a little getting used to. It could also be made a little more clear by seperating the anonymous method out, and or cleaning up the formatting a bit. As I experiment with it more, hopefully I'll figure out the best way to set everything up to be readable as well as useful.

# re: Converter<T> and ConvertAll()

Monday, April 25, 2005 9:41 AM by Sean Chase    
Holy smokes Steve, I feel like I just got done tweaking my eyes to focus in on one of those 3D art pictures. I'm gonna have to take a longer look at this later. :-)

# re: Converter<T> and ConvertAll()

Monday, April 25, 2005 11:20 AM by Steve    
LOL, focus hard and you'll see a picture of Bill Gates ;-)

# re: Converter<T> and ConvertAll()

Monday, April 25, 2005 5:20 PM by Senkwe    
Hmm, I find it very readable. Trust me, a once over with a basic Anonymous Methods tutorial and it'll just &quot;click&quot;.

# re: Converter<T> and ConvertAll()

Monday, April 25, 2005 10:44 PM by Steve    
Agreed, it does take that tutorial before it will click though.

# re: Converter and ConvertAll()

Wednesday, March 25, 2009 3:46 AM by Colorado real estate    
You really are out of touch aren't you? Unions have been the vast minority for a really long while. I've never been in a union.

Post a Comment

 
 
Prove you're not a spammer: 
4 + 4 =