Dictionary Class Demo in C#

The Dictionary<TKey, TValue> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary<TKey, TValue> class is implemented as a hash table.


As long as an object is used as a key in the Dictionary<TKey, TValue>, it must not change in any way that affects its hash value. Every key in a Dictionary<TKey, TValue> must be unique according to the dictionary's equality comparer. A key cannot be null, but a value can be, if the value type TValue is a reference type.
Dictionary<TKey, TValue> requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer<T> generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer<T>.Default is used. If type TKey implements the System.IEquatable<T> generic interface, the default equality comparer uses that implementation.


C# :

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DictionaryDemoWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        Dictionary<string, string[]> countryList = new Dictionary<string, string[]>();
        protected void Page_Load(object sender, EventArgs e)
        {
            countryList["India"] = new string[] { "Pune", "Mumbai" };
            countryList["USA"] = new string[] { "California", "Boston" };
            countryList["Germany"] = new string[] { "Hamberg", "Berlin" };
            if (!IsPostBack)
            {
               
                foreach (string CountryKey in countryList.Keys)
                {
                    ddlCountry.Items.Add(CountryKey);
                }
            }
        }
        protected void ddlUniv_SelectedIndexChanged(object sender, EventArgs e)
        {              
        }      
        protected void countrycomboBox_SelectedIndexChanged(object sender,EventArgs e)
        {
            string selectedCountry = ddlCountry.SelectedItem.Value.ToString();      
            string[] sel = countryList[ddlCountry.SelectedItem.Text];
            ddlUniv.DataSource = sel;
            ddlUniv.DataBind();      
        }
    }
}


aspx :

<asp:Label ID ="lblCountry" runat ="server"></asp:Label>
    <asp:DropDownList ID ="ddlCountry" runat="server" OnSelectedIndexChanged="countrycomboBox_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
    <br />
    <br />
    <asp:Label ID ="Label1" runat ="server"></asp:Label>
    <asp:DropDownList ID ="ddlUniv" runat="server"
            onselectedindexchanged="ddlUniv_SelectedIndexChanged"></asp:DropDownList>

Comments

Popular posts from this blog

Add Serial no in crystal report without coding

Working with Oracle's BLOB and Microsoft's C#

File operations in C# (.net)