Yes,we know index based collections like array,arraylist, as well as list also supports the elements as indexs. now we demonstract what is key/ value collections.
whatever collections which implements IDictionary interface. those are all comes under key/value pair collections.
Ex: HastTable,SortedList.....ect
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1.02, "anand");
ht.Add("2", "balu");
ht.Add("abcd", "charan");
IDictionaryEnumerator ider=ht.GetEnumerator() ;
while (ider.MoveNext())
{
DictionaryEntry iers=ider.Entry;
Console.WriteLine("key is: " + iers.Key + " values is: " + iers.Value);
}
Console.ReadKey();
}
}
Here hashtable takes any type data accepts as key and as value because
public virtual void Add(object key, object value);
Note: In int,float,double...we know these all arrays accepts elements as particular type only but coming these collections can accepts any value as key and any value as value.
Advantages:
using these collections we can easily retrive the data as possible as early like in databases index.
Avoiding duplicate keys in a collection.
Avoiding null value as key.
so every value must require a key.
these are all similar to database table constraints know. so we can consider key is index of primary key table in database.
SortedList:
SortedList also implements IDictionary interface but which will implictly sorts all the elements in the collection.
Note: SortedList accepts any value as key and any value as value but all the keys should be same type.it can be int,string,double...ect.
key can't be null
key can't be redundent.
EX:
SortedList sorlist = new SortedList();
sorlist.Add("ravi", "kasibabu");
sorlist.Add("ca", "dafs");
sorlist.Add("a", "sadfasd");
Console.WriteLine(sorlist.Count);
IDictionaryEnumerator ider=sorlist.GetEnumerator() ;
while (ider.MoveNext())
{
DictionaryEntry iers = ider.Entry;
Console.WriteLine("key is: " + iers.Key + " values is: " + iers.Value);
}
Console.ReadKey();
Yes,here is one more thing is we can Observe Abstraction also?
Are we comparing keys my ourself?
these key can be int,string,double.....ect
whatever collections which implements IDictionary interface. those are all comes under key/value pair collections.
Ex: HastTable,SortedList.....ect
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1.02, "anand");
ht.Add("2", "balu");
ht.Add("abcd", "charan");
IDictionaryEnumerator ider=ht.GetEnumerator() ;
while (ider.MoveNext())
{
DictionaryEntry iers=ider.Entry;
Console.WriteLine("key is: " + iers.Key + " values is: " + iers.Value);
}
Console.ReadKey();
}
}
Here hashtable takes any type data accepts as key and as value because
public virtual void Add(object key, object value);
Note: In int,float,double...we know these all arrays accepts elements as particular type only but coming these collections can accepts any value as key and any value as value.
Advantages:
using these collections we can easily retrive the data as possible as early like in databases index.
Avoiding duplicate keys in a collection.
Avoiding null value as key.
so every value must require a key.
these are all similar to database table constraints know. so we can consider key is index of primary key table in database.
SortedList:
SortedList also implements IDictionary interface but which will implictly sorts all the elements in the collection.
Note: SortedList accepts any value as key and any value as value but all the keys should be same type.it can be int,string,double...ect.
key can't be null
key can't be redundent.
EX:
SortedList sorlist = new SortedList();
sorlist.Add("ravi", "kasibabu");
sorlist.Add("ca", "dafs");
sorlist.Add("a", "sadfasd");
Console.WriteLine(sorlist.Count);
IDictionaryEnumerator ider=sorlist.GetEnumerator() ;
while (ider.MoveNext())
{
DictionaryEntry iers = ider.Entry;
Console.WriteLine("key is: " + iers.Key + " values is: " + iers.Value);
}
Console.ReadKey();
Yes,here is one more thing is we can Observe Abstraction also?
Are we comparing keys my ourself?
these key can be int,string,double.....ect
now we just passing similar type of data as a key to sortedlist collection which will implictly compares the keys each other this done by implictly this is called abstraction process.
as well as sortedlist accepts hashtable as parameter to sortedlist initialization but hasttable all keys should be same type.
Initializes a new instance of the System.Collections.SortedList class that
contains elements copied from the specified dictionary, has the same initial
capacity as the number of elements copied, and is sorted according to the
System.IComparable interface implemented by each key.
No comments:
Post a Comment