Click here to Skip to main content
12,695,743 members (33,534 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked

Sorted Dictionary C# 2.0

, 6 Mar 2006
Rate this:
Please Sign up or sign in to vote.
Using the SortedDictionary for a contact list.

Sample Image


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.


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".

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());
      // 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.

// 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
    ICollection valueColl = Table.Values;
    foreach (object val in valueColl)
       //Console.WriteLine("Value = {0}", val);
    foreach (Person key in Table.Keys)

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#.


  • Version 1.0 - 3/4/06.


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalefficiency Pin
Sameer Alibhai21-Jun-07 5:49
memberSameer Alibhai21-Jun-07 5:49 
GeneralRe: efficiency Pin
Guerven13-Aug-08 0:02
memberGuerven13-Aug-08 0: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 6 Mar 2006
Article Copyright 2006 by wws35801
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid