Click here to Skip to main content
15,881,589 members
Articles / Programming Languages / C#
Article

Sorted Dictionary C# 2.0

Rate me:
Please Sign up or sign in to vote.
1.44/5 (9 votes)
6 Mar 2006 60.2K   330   13   2
Using the SortedDictionary for a contact list.

Sample Image

Introduction

This article is an attempt to add the C# 2.0 SortedDictionary logic to the article by Liong Ng: XML Serialization in .NET. A portion of the original article is deleted for ease of analysis. I only load data from a tab delimited file and process this data.

Background

I find very little on implementing, and through this article, hope to start a dialogue on, the use of generics and SortedDictionary. The impetus was for a class project.

Using the code

The Person, PhoneNumber, and Contact classes are all basically as developed in the cited article. The Contacts class is where most of the changes have been made. We start by loading in firstName, lastName, and phoneNumber from a tab delimited file "ContactList.tsv".

C#
class Contacts  
{
    // Encapsulate table
    private SortedDictionary<Person, Contact> table = 
             new SortedDictionary<Person, Contact>();

    public SortedDictionary<Person, Contact> Table  
    {
      get { return table; }
      set { table = value; }
    }
    ....
}

private static SortedDictionary<Person, Contact> 
        AddEntry(SortedDictionary<Person, Contact> Table) 

{
   if (File.Exists("ContactList.tsv"))
   {
     // Open File
     StreamReader re = File.OpenText("ContactList.tsv");
     .....
     if (Table.ContainsKey(person))
        {
          // show names that have duplicate last name
          Console.WriteLine("Person Exists  " + person.ToString());
        }
      else
      // add new contact with person as key
      Table.Add(Person, Contact);
   }
   ......
}

The display of the contact list uses a foreach to loop through the table.

C#
// display dictionary content
private static void DisplayDictionary<Person, Contact>
                    (SortedDictionary<Person, Contact> Table)
{
    // Several ways to write data are ahown
    // generate output for each key in the sorted dictionary
    // by iterating through the Keys property with a foreach statement
    foreach (KeyValuePair<Person, Contact> kvp in Table)
    {
       Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
    }

    foreach (object de in Table) //DictionaryEntry
    {
       Console.WriteLine(de.ToString());
    }
    ICollection valueColl = Table.Values;
    foreach (object val in valueColl)
    {
       //Console.WriteLine("Value = {0}", val);
       Console.WriteLine(val);
    }
    foreach (Person key in Table.Keys)
    {
       Console.WriteLine(key.ToString());
    }

Points of Interest

I made several attempts to get foreach to work correctly. I would appreciate any and all constructive comments. This is my first project in C#.

History

  • Version 1.0 - 3/4/06.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalefficiency Pin
Sameer Alibhai21-Jun-07 4:49
Sameer Alibhai21-Jun-07 4:49 
Good work.

A little not so well known fact, is that foreach is less efficient then say using:

for (int i=0; i < items.length ; i++) <br />
{ <br />
//do something with items[i]; <br />
}<br />


But unless performance is very important in one case, i would still use foreach in most cases because its much easier and cleaner too

Reference: Improving ASP.NET Performance[^]

G
GeneralRe: efficiency Pin
Guerven12-Aug-08 23:02
Guerven12-Aug-08 23:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.