Search
Monday, September 10, 2001 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
     
  
   TechTidBits (Blog)  

Generics, and searching for a needle in the haystack

Oct 16

Written by:
Thursday, October 16, 2008 9:21 PM  RssIcon

I've been using Generics for a little while now (List<Team> = new List<Team>()); and I'm liking them quite a bit.  I LOVE the strong typing it gives me, It's ALWAYS better to find bugs at compile time than at runtime.  But I always forget the predicate syntax for anonymous methods.  So here it is for all posterity.

Here are the details on the Generic find and a Sort thrown in for good measure.  The sample Team class to follow at the end.

This is a sample of the anonymous method used for the find predicate and the sort as well.

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            List teams = new List();
            Team canadiens = new Team("Montreal Canadiens", "Habs", 1);
            teams.Add(canadiens);
            Team senators = new Team("Ottawa Senators", "Sens", 99);
            teams.Add(senators);
            Team sabres = new Team("Buffalo Sabres", "Sabres", 5);
            teams.Add(sabres);
            Team leafs = new Team("Toronto Maple Leafs", "Leafs", 75);
            teams.Add(leafs);
            Team bruins = new Team("Boston Bruins", "Bruins", 5);
            teams.Add(bruins);

            Console.WriteLine(Environment.NewLine + "Teams as added to the List");
            foreach (Team team in teams)
            {
                Console.WriteLine("Team Name = " + team.TeamName + " (" + team.Nickname + ")");
            }

            Console.WriteLine(Environment.NewLine);

            Team myFavourite = teams.Find(delegate(Team team)
            {
                return (String.Compare(team.Nickname, "HABS", true) == 0);
            });
            Console.WriteLine("My favourite team is the " + myFavourite.TeamName);
            Console.WriteLine(Environment.NewLine + "\t\tGO " + myFavourite.Nickname + " GO!");

            Console.WriteLine(Environment.NewLine + "Order the teams by default sort order");
            teams.Sort();
            foreach (Team team in teams)
            {
                Console.WriteLine(team.Rating + ") " + team.Nickname);
            }

            teams.Sort(TeamSortByRating);
            Console.WriteLine(Environment.NewLine + "Order the teams by rating");
            foreach (Team team in teams)
            {
                Console.WriteLine(team.Rating + ") " + team.Nickname);
            }

            Console.ReadLine();
        }

        public static int TeamSortByRating(Team team1, Team team2)
        {
            if (team1.Rating == team2.Rating)
            {
                return team1.TeamName.CompareTo(team2.TeamName);
            }
            else
            {
                return team1.Rating.CompareTo(team2.Rating);
            }
        }
    }
}

 

namespace TestConsole
{
    public class Team : IComparable
    {
        public string TeamName
        {
            get;
            set;
        }

        public string Nickname
        {
            get;
            set;
        }

        public int Rating
        {
            get;
            set;
        }

        public Team(string teamName, string nickname, int rating)
        {
            TeamName = teamName;
            Nickname = nickname;
            Rating = rating;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            return this.TeamName.CompareTo(((Team)obj).TeamName);
        }

        #endregion
    }
}

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement