Click here to Skip to main content
Click here to Skip to main content

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

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

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.

// 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

Share

About the Author

wws35801

United States United States
No Biography provided

Comments and Discussions

 
Generalefficiency PinmemberSameer Alibhai21-Jun-07 5:49 
GeneralRe: efficiency PinmemberGuerven13-Aug-08 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.1411019.1 | Last Updated 6 Mar 2006
Article Copyright 2006 by wws35801
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid