Arrays.
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 :
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:
You can also add elements at the time of initialization using object initializer syntax as below:
AddRange:
Note :The AddRange() method will only be applicable if you initialized with a List<T> variable. IList<T> doesn't include the AddRange() method.
- 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
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 :
- List<T> stores elements of the specified type and it grows automatically.
- List<T> can store multiple null and duplicate elements.
- 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
- List<T> can be access using indexer, for loop or foreach statement.
- LINQ can be use to query List<T> collection.
- 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)
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:
Post a Comment