What is a List<T>?
List<T> is a generic class in the System.Collections.Generic Namespace. An instance of this class is basically a strongly typed arraylist (that is, the type of the items in the collection is fixed at the time of instantiation. )
Consider this example
ArrayList leaderNames = new ArrayList();
leaderNames.Add("Obama");
leaderNames.Add(5);
Notice that this array list consists of a string and an int. That is it is NOT strongly typed.
In the above example if I want to allow just strings to be entered. I will use a strongly typed list as shown below.
List<string> leaderNames = new List<string>(); //Notice here that I am specifying that the List is a List of strings
leaderNames.Add("Obama");
leaderNames.Add("VPutin");
Trying something like this will throw a compilation error.
leaderNames.Add(7);// will throw error because 7 is not a string
An example that uses List<T>
There are many ready made methods for your use, that comes with with an instance of List<T> class.
The example below shows some things that you can do with an instance of the List<T> class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> leaderNames = new List<string>();
leaderNames.Add("Obama");
leaderNames.Add("VPutin");
List<string> womenLeaders = new List<string>();
womenLeaders.Add("HillaryClinton");
womenLeaders.Add("SarahPalin");
leaderNames.AddRange(womenLeaders);//Increase the range of the existing array by adding more items into the List
foreach (string leader in leaderNames)
{
Console.WriteLine(leader);
}
/*This is how the output would look like
Obama
VPutin
HillaryClinton
SarahPalin
Press any key to continue . . .
*/
leaderNames.Clear();//Clear all the items
}
}
}
How to sort a List<T>?
To custom sort a List<T> all you need to do is have a class that implements the IComparer<T> interface. When you implement this method, all you need to do is write a logic that
- throws an output of 1 when the second input parameter to the Compare() Method is greater
- throw an output of -1 when the first input parameter is greater and
- 0 when they are equal.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Car c1 = new Car("Mustang", 210);
Car c2 = new Car("Honda", 110);
Car c3 = new Car("Veyron", 1000);
List<Car> Cars = new List<Car>();
Cars.Add(c1);
Cars.Add(c2);
Cars.Add(c3);
Console.WriteLine("---Before Sort----");
foreach (Car c in Cars)
{
Console.WriteLine(c.model);
}
IComparer<Car> cSort = new CarSorter();
Cars.Sort(cSort);
Console.WriteLine("---After Sort----");
foreach (Car c in Cars)
{
Console.WriteLine(c.model);
}
}
}
class Car
{
public Car(string Model, int HorsePower)
{
model = Model;
horsePower = HorsePower;
}
public string model
{
get;
set;
}
public int horsePower
{
get;
set;
}
}
class CarSorter : IComparer<Car>
{
public int Compare(Car x, Car y)
{
if (y.horsePower > x.horsePower)
{
return 1;
}
else if (x.horsePower > y.horsePower)
{
return -1;
}
else
{
return 0;
}
}
}
}
How to find an element in the List?
Check this out for an example on find in list.
http://dotnetanalysis.blogspot.com/2011/12/predicate-c.html
What is the difference between Add and Insert?
Add() adds the item to the bottom of the list. Insert adds the item at the specified index and pushes down the other items starting at that index.
http://dotnetanalysis.blogspot.com/2013/05/introduction-to-generics-in-c.html
NIce
ReplyDeleteGood
ReplyDelete