Working with List in C#
Objective
When you have a number of objects that you want to manipulate, you can choose many type of methods to support containing and doing manipulation. One of the best choices is the List class. This tutorial will show you how to use List class in working with collection of objects.
Let's go
List
Although you can use array to do many things with collection of objects, but array has exposed an limitation, that is array doesn’t allow flexible extension. When you use an array, you must declare length of array before you can use. List comes across this problem and you can freely extend length of collections as you want. The default length of list is 16, but if you add more than 16 elements to the list, so it dynamically extends to adapt. You can also declare length of the list by using overload Constructor when you declare the list or by setting Capacity property before you use the list to increase the performance. When you declare a list, you also specify the type of object in the list. For example:
You can also specify length of list as follows:
After you have initialized an list, you can add element to list. Table bellow list some members that you can use:
Name | Description |
Capacity | Gets or sets the total number of elements the list can contain without resizing |
Count | Gets the total number of elements actually contained in the list |
Add | Adds an object to the end of the list |
AddRange | Adds a collection of objects to the end of the list |
BinarySearch | Searches a sorted list for a value using a binary search |
Clear | Removes all elements from the list |
Contains | Determines whether an element is in the list |
Exists | Determines whether the list contains elements that match the conditions specified |
Find | Searches for an element that matches the conditions defined and returns the first occurrence within the entire list |
FindAll | Retrieves all the elements that match the conditions defined |
ForEach | Performs the specified action on each element in the list |
Sort | Sorts the elements in the list |
TrimExcess | Sets the capacity to the actual number of elements in the list |
TrueForAll | Determines whether every element in the list matches the conditions defined |
LinkedList
In case of you want to quickly insert/add elements to a list, you can use LinkedList. This class only allows sequentially access. For random access you should use List. An example of LinkListed is listed as follows:
Note that LinkedList don’t allow specifies length of list in Constructor member.
Working with List
To understanding the use of the list in C#, let see the example code bellows:
- //declare a list object
- //add elements to list
- lInt.Add(5);
- lInt.Add(10);
- lInt.Add(50);
- lInt.Add(70);
- //write number of elements in list
- Console.WriteLine("Length of the list " + lInt.Count);
- //remove 2nd element
- lInt.RemoveAt(2);
- //write number of elements in list
- Console.WriteLine("Length of the list " + lInt.Count);
Length of the list 3
Explanation In this above code:
- We simply declare an list object and add value (5,10,50,70) to the list by using Add member of list
- We then get and write the length of list by using Count member of list
- Lastly we remove the 2nd element in the list and then write length of list to check whether if we have removed successfully.
Summary
In this tutorial you leart of using List class in working with collection. Working with Collection in program is problem that you will get very usually, so you must try to investigate it carefully. Make sure you are choosing right way to do with collection based on the type of collection you are doing with, it’s the key to the success.
Add new comment
- 620 views