C# Generic Lists with Complete Programming Example

In this chapter you will learn

  1. What are Generic Lists?
  2. How it is different from Collection List
  3. Programming Example

What are Generic Lists

The Generic Lists are similar to arrays. You can add, delete, populate, sort Generic Lists. is a placeholder for data types and replaced by datatype at compile time.

How it is different from Collection Lists?

However, function of the both lists are same but Generic Lists are Type Safe because it is strongly bound by data type. Unlike Collection lists you don’t need to boxing and unboxing Generic Lists.

How to declare Generic Lists

You can declare Generic Lists in following manner:

List intList = new List();

Important Function of Lists

ItemsGets or sets the element at the specified index
CountReturns the total number of elements exists in the List
AddAdds an element at the end of a List.
AddRangeAdds elements of the specified collection at the end of a List.
BinarySearchSearch the element and returns an index of the element.
ClearRemoves all the elements from a List.
ContainsChecks whether the speciied element exists or not in a List.
FindFinds the first element based on the specified predicate function.
ForeachIterates through a List.
InsertInserts an element at the specified index in a List.
InsertRangeInserts elements of another collection at the specified index.
RemoveRemoves the first occurence of the specified element.
RemoveAtRemoves the element at the specified index.
RemoveRangeRemoves all the elements that match with the supplied predicate function.
SortSorts all the elements.
TrimExcessSets the capacity to the actual number of elements.
TrueForAllDetermines whether every element in the List matches the conditions defined by the specified predicate.

Programming Example


Steven Clark Jack
After Sorting
Clark Jack Steven
Removing Clark
Jack Steven
Inserting Mathew at index position 2
Jack Steven Mathew


In this chapter you learned Generic Lists with complete and easy programming example. In the next chapter you will learn Generic Dictionary class with complete programming example.

Leave a Reply

Your email address will not be published. Required fields are marked *