In which situation we should use Array, ArryList,HashTable or SortedList

Posted by Ray.chayan under C# on 6/28/2010 | Views : 4336 | Status : [Member] | Replies : 5
We know that they(Array, ArryList,HashTable or SortedList ) are all collection types. But

when we should use array ,when arraylist, when hashtable and when sortedlist




Responses

Posted by: Vuyiswamb on: 6/28/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
There is no place in my applications where i use ArrayList, they are outdated and they are slow and they are replaced by LIST<type>. It depends on the Programmer. i list hash Tables and List they are Fine and they all have advantages and disadvantages. But for performance boast, look at the Lists instead of an ArrayList.
more info on them look at this
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx and about hashtables look at this
http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

Thank you for posting at Dotnetfunda

Vuyiswa Maseko


Thank you for posting at Dotnetfunda
[Administrator]

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 7/16/2010 [Member] Bronze

Up
0
Down
Array When you are aware that how many objects are going to come you should use array. Array is not having the direct comparison like the ArrayList has with the method Contains(<valueToCheck>)

ArryList When you are don't know how many element are going to be added in the object you can use ArrayList. You can use the Contains method to compare the list.


HashTable When you want to use the Key and value type of data you can use it. Like Roll Number and Student.

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ray.chayan on: 7/16/2010 [Member] Starter

Up
0
Down
What is the facility of using Hashtable over array list?
What is the facility of using arraylist over array ?

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 7/17/2010 [Member] Bronze

Up
0
Down
Facility of using Hashtable over array list
1. You will get the key and value pair for example you want to store the data related to the currency abbreviation and the name of the currency you cannot perform the proper operation with ArrayList so you can use Hashtable

Hashtable curr = new Hashtable();

//Now add the currency details
if(!curr.Keys.Contains("INR"))
{
curr.Add("INR", "INDIAN RUPEES");
}

this thing is not possible in ArrayList

Facility of using arraylist over array
You can get the details related to added element directly in ArrayList
ArrayList arrList = new ArrayList();

arrList.Add(15);
arrList.Add(16);
arrList.Add(17);
arrList.Add(19);

Now check before adding the object
i
f(!arrList.Contains(15))

{
arrList.Add(15);
}
else
{
MessageBox.Show("Already Exists");
}


and if you want to do the same task in array, you have to iterate the elements and get the desired outp ut

Thanks,


Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay13mar on: 10/27/2012 [Member] Starter | Points: 25

Login to post response