The current project I am working on has been my first chance to work with .NET 2.0 in a real world environment and I have to say that I am really digging the generic list functions. Check out the hockey player class below:
public class HockeyPlayer { private int number; private string name; private int age; private string position; public HockeyPlayer(int number, string name, int age, string position) { this.number = number; this.name = name; this.age = age; this.position = position; } public int Number { get { return number; } set { number = value; } } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public string Position { get { return position; } set { position = value; } } }
An easy way of finding all draft eligble players (players over 18 year of age) in a list is to take advantage of delegates and the methods in the generic list class:
[Test] public void ShouldFindAllDraftEligibleHockeyPlayers() { List<HockeyPlayer> hockeyPlayers = new List<HockeyPlayer>(); hockeyPlayers.Add(new HockeyPlayer(10, "Angelo Esposito", 17, "Left Wing")); hockeyPlayers.Add(new HockeyPlayer(9, "Andrew Cogliano", 19, "Left Wing")); hockeyPlayers.Add(new HockeyPlayer(10, "Jonathan Toews", 18, "Center")); hockeyPlayers.Add(new HockeyPlayer(24, "Sam Gagner", 17, "Right Wing")); List<HockeyPlayer> draftEligiblePlayers = hockeyPlayers.FindAll(delegate(HockeyPlayer player) { return player.Age < 18; }); Assert.AreEqual(draftEligiblePlayers.Count, 2); }
The delegate method searches through the list and finds all draft eligible players, this saves you the time of creating a for loop to search through the list.
[ Currently Playing : Shampoo Suicide - Broken Social Scene - Half Nelson (Original Motion Picture Soundtrack) (04:07) ]
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Steven Rockarts
E-mail