Sunteți pe pagina 1din 4

Arraylist,Hash table,Sorted list and Dictionary C#.

Net
In this article we will discuss about Arraylist, Hash table, Sorted list and Dictionary.
Arraylist:
- This is one type of Array whose size can increase and decrease dynamically.
- Arraylist can hold items of different types.
- The base class is System.Collections.ArrrayList.
- An ArrayList uses an array internally and initializes its size with a default value called capacity.
As the no of elements increases or decreases , it adjust the capacity of the array by making a
new array and copying the old values into it.
ArrayList list = new ArrayList();
To add elements in ArrayList
list.add("Raju");
list.add("biju");
To remove the element from the arraylist
list.Remove("raju");
To remove the element at index 1
list.Remove(1);
To remove three elements starting at index 4
list.RemoveRange(4,3);
Hash table:
- Hash table provides a way of accessing the index using a user identified key value.
- It removes the index problem.
- It stores the items as key value pairs.Each item(value) in the hash table is uniquely identified by
its key and its value as an object type.
- Mostly string class is used as the key in hash table but we can also use other class as key.
Hashtable myht = new Hashtable();
myht.Add("one","The");
myht.Add("two","quick");
Here 'two' and 'one' are the keys and 'The' and 'quick' are the values.
PrintKeysAndValues(myht);
To remove elements with the key "two"
myht.Remove("two");
Sorted list:
Simillar to Hash table, the difference is that the values are sorted by keys and acessible by key as
well as by index.
SortedList mysl = new SortedList();
mysl.Add("one","The");
mysl.Add("two","quick");
Dictionary:
- A kind of collection that stores items in a key-value pair fashion.
- Each value in the collection is identified by its key.
- All the keys in the collection are unique and there cannot be more than one key with the same
name.
- The most common types of dictionary in the system.collection name space are Hash table and
the sorted list.

Difference between Hashtable and Dictionary


Hashtable and Dictionary are collection of data structures to
hold data as key-value pairs. Dictionary is generic type, hash
table is not a generic type. The Hashtable is a weakly typed
data structure, so you can add keys and values of any Object
Type to the Hashtable. The Dictionary class is a strongly types
< T Key, T Value > and you must specify the data types for
both the key and value.

C# Declaration
Hashtable numbers = new Hashtable();
Dictionary<int, string> dictionary = new Dictionary<int, string >();

VB.Net Declaration
Dim numbers As New Hashtable()
Dim dictionary As New Dictionary(Of Integer, String)()

While perform operations Dictionary is faster because there is


no boxing/unboxing (valuetypes don't need boxing) while in
Hashtable boxing/unboxing (valuetypes need boxing) will
happened and which may have memory consumption as well
as performance penalties.
Another important difference is that Hashtable Hashtable is
thread safe for supports multiple reader threads and a single
writer thread. That is the Hashtable allows ONE writer together
with multiple readers without locking. In the case of Dictionary
there is no thread safety, if you need thread safety you must
implement your own synchronization.
When we add the multiple entries in Dictionary, the order in
which the entries are added is maintained. When we retrieve
the items from Dictionary we will get the records in the same
order we have inserted them. Whereas we add same records in
Hashtable the order is not maintained. From the following
example you can see the difference in the order of retrieval.

using System;
using System.Collections.Generic;
using System.Collections ;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Hashtable numbers = new Hashtable();
numbers.Add(1, "one");
numbers.Add(2, "two");
numbers.Add(3, "three");
numbers.Add(4, "four");
numbers.Add(5, "five");
foreach (DictionaryEntry num in numbers)
{
MessageBox.Show(num.Key + "
" + num.Value);
}
Dictionary<int, string> dictionary = new Dictionary<int,
string >();
dictionary.Add(1,"one");
dictionary.Add(2,"two");
dictionary.Add(3,"three");
dictionary.Add(4,"four");
dictionary.Add(5, "five");
foreach (KeyValuePair<int,string> pair in dictionary)
{
MessageBox.Show(pair.Key + "
" + pair.Value);
}
}
}
}

How to C# Dictionary
A Dictionary class is a data structure that represents a
collection of keys and values pair of data. The key is identical
in a key-value pair and it can have at most one value in the
dictionary, but a value can be associated with many different
keys. More about.... C# Dictionary

Iterate over a Dictionary


There are many different ways to iterate over a Dictionary in
C#. From the following link you can see in detail .... How to
Iterate over a Dictionary

Dictionary Versus List


Both lists and dictionaries are used to store collections of data.
The Dictionary is based on a hash table, that means it uses a
hash lookup, which is a rather efficient algorithm to look up
things, on the other hand, a list you have to go element by
element until it finds the result from beginning to the result
each time. More about....Dictionary vs List

S-ar putea să vă placă și