Sunday 6 November 2016

Array Vs ArrayList Vs List

Arrays.

 Arrays belongs to  System.Array

-Arrays are collection of same data type.(i.e. strongly typed)

 Arrays are fixed in length that cannot be changed during runtime.

 Generally, In arrays we will store the values with the index basis that will start with zero.  
 If you want to access values from arrays we need to pass index values.

 Look at the below example.


  
In the above example i have declared size as 2. Let's try to add one more item here .


It will gives the run time exception. Index out of Range Exception, Because we mentioned the size as 2 now we are adding 3rd element to same array. 

---------------------------------------------------------------------------------------------------------------


ArrayLists

ArrayLsits belongs to System.Collections.

ArrayLists are not strongly typed collection. It will store the values of Same data type or different data type.

ArrayLsits size will increase or decrease dynamically.




The limitation of these collections is that while retrieving items, you need to cast into the appropriate data type, otherwise the program will throw a runtime exception. It also affects on performance, because of boxing and unboxing.
To overcome this problem, C# includes generic collection classes in the System.Collections.Generic namespace.

------------------------------------------------------------------------------------------------------------



List<T>

The List<T> collection is the same as an ArrayList except that List<T> is a generic collection whereas ArrayList is a non-generic collection.


Points to Remember :

  1. List<T> stores elements of the specified type and it grows automatically.
  2. List<T> can store multiple null and duplicate elements.
  3. List<T> can be assigned to IList<T> or List<T> type of variable. It provides more helper method When assigned to List<T> variable
  4. List<T> can be access using indexer, for loop or foreach statement.
  5. LINQ can be use to query List<T> collection.
  6. List<T> is ideal for storing and retrieving large number of elements.


List<T> can be initialized in the following two ways.



Add Elements into List:


Use the Add() method to add an element into a List collection. The following example adds int value into a List<T> of int type.

Add() signature: void Add(T item)


You can also add elements at the time of initialization using object initializer syntax as below:




AddRange:


The AddRange() method adds all the elements from another collection.
AddRange() signature: void AddRange(IEnumerable<T> collection)

Note :The AddRange() method will only be applicable if you initialized with a List<T> variable. IList<T> doesn't include the AddRange() method.

Access List collection:

- Use a foreach or for loop to iterate a List<T> collection.

Example


- If you have initialized the List<T> with an IList<T> interface then use seperate foreach statement with implicitly typed variable:

Example

- Access individual items by using an indexer (i.e., passing an index in square brackets):

Example



Use the Count property to get the total number of elements in the List.
Example



Use for loop to access list as shown below:
Example :Accessing List using for Loop Example











Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      










No comments: