.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Generics >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Working with the Generic Dictionary Class In C#

Posted By:Hefin Dsouza       Posted Date: August 11, 2009    Points: 10    Category: Generics    URL: http://www.dotnetspark.com  

Working with the Generic Dictionary Class In C#

This Example Demonstrates the use Of a Generic Dictionary Class Which is an Improved HashTable Class and performes much better than a HashTable.

Example :

using System;
using System.Collections.Generic;
class Program
        static void Main(string[] args)
            Dictionary< int, string > students = new Dictionary< int, string >();
            students.Add(1, "Hefin");
            students.Add(2, "Abbas");
            students.Add(3, "Rinso");
            students.Add(4, "Sachin");

            Dictionary< string, string > Employees = new Dictionary< string, string >();
            Employees.Add("E01", "Hefin Dsouza");
            Employees.Add("E02", "Abbas Electriwala");
            Employees.Add("E03", "Rinso Joseph");
            Employees.Add("E04", "Sachin Gaikar");

            Console.WriteLine("List of all Students");
            foreach (KeyValuePair< int,string > n in students)
                Console.WriteLine("|Roll Number : " + n.Key + "t| Name : " + n.Value);

            Console.WriteLine("List of all Employees");
            foreach (KeyValuePair< string, string > n in Employees)
                Console.WriteLine("|EMP ID : " + n.Key + "tt| EMP Name : " + n.Value);

This is an Improved HashTable wich restricts what datatype will the Key be and WHat DataType the value should be.So it makes this class much Type Safe.

Regards Hefin Dsouza


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend