Some My Experiences

Header Ads

Thursday 17 March 2016

List in C#

List Class defined in System.Collections.Generic Namespace in mscorlib.dll assembly, it provide methods to search, sort and manipulate lists. Elements in List can be accessed using an integer index, index start from 0 (zero-based). We can create object list from this class with syntax :
List<T>
where T is The type of elements in the list, example : List<int>.



Defining a list
Defining List which holds integer value:
List<int> listInt = new List<int>();
Defining List which holds string value:
List<string> listString = new List<string>();
Defining List with Object Initializer
List<string> listAnimal = new List<string>{ "Cat", "Dog",  "Mouse" };

Gets the number of elements in the List
We can use Count property to get the number of items in the list, example : listAnimal.Count will produce 3.

Retrieve items from list
We can get list item using index, example : listAnimal[0]. We will get "Cat".
Loop with foreach
foreach (string animal in listAnimal)    {
        Console.WriteLine(animal);

}
Loop with for
for (int i = 0; i < listAnimal.Count; i+)
{
        Console.WriteLine(listAnimal[i]);

}

Insert an item to the list
listAnimal.Insert(1, "Monkey");
here we insert Monkey in to the list at the 1 index, so listAnimal now contains "Monkey" with orders : Cat, Monkey, Dog, Mouse.

Ordering items in the List
We can reordering items in the list using Sort method. Example :
listAnimal.Sort();
After using Sort method, listAnimal become Cat, Dog, Monkey, Mouse. This method use ascending sort as default order. We can sort items with ascending or descending order with Lambda.
Ordering items with ascending sort, this is equivalent with Sort method without parameter:
listAnimal.Sort((a, b) => a.CompareTo(b));
Ordering items with descending sort :
listAnimal.Sort((a, b) => -1* a.CompareTo(b));
or equivalent to
listAnimal.Sort((a, b) => b.CompareTo(a));

Code For Sort:


using System;
using System.Collections.Generic;

namespace LexOne.List
{
   class TestSortList
   {
     
      static void Main(string[] args)
      {
         List<string> listAnimal = new List<string>{ "Cat", "Monkey", "Dog",  "Mouse" };
         Console.WriteLine("Before Sort:");
         foreach (string animal in listAnimal)    {
            Console.WriteLine(animal);
         }
         Console.WriteLine("");
        
         Console.WriteLine("After using Sort Method without parameter (ascending):");
         listAnimal.Sort();/* No parameter, default ascending sort */
         foreach (string animal in listAnimal)    {
            Console.WriteLine(animal);
         }
        
         Console.WriteLine("");
         Console.WriteLine("After using Sort Method with lambda (ascending):");
         listAnimal.Sort((a, b) => a.CompareTo(b));/* ascending sort */
         foreach (string animal in listAnimal)    {
            Console.WriteLine(animal);
         }
        
         Console.WriteLine("");
         Console.WriteLine("After using Sort Method with lambda (descending):");
         listAnimal.Sort((a, b) => -1 * a.CompareTo(b));/* descending sort */
         foreach (string animal in listAnimal)    {
            Console.WriteLine(animal);
         }
        
         Console.WriteLine("");
         Console.WriteLine("After using Sort Method with lambda (descending):");
         listAnimal.Sort((a, b) => b.CompareTo(a));/* descending sort */
         foreach (string animal in listAnimal)    {
            Console.WriteLine(animal);
         }
        
         Console.ReadKey();
      }
   }
}

Output:
Before Sort:
Cat
Monkey
Dog
Mouse

After using Sort Method without parameter (ascending):
Cat
Dog
Monkey
Mouse

After using Sort Method with lambda (ascending):
Cat
Dog
Monkey
Mouse

After using Sort Method with lambda (descending):
Mouse
Monkey
Dog
Cat

After using Sort Method with lambda (descending):
Mouse
Monkey
Dog
Cat

1 comment: