using System;
using System.Collections.Generic;
/*List is one of the generic collection classes present in System.Collections.Generic
* A List class can be used to create a coleection of any type
* The objects stored in the list can be accessed by index
* This class also provides methods to search, sort, and manipulate lists
* */
class Program
{
static void Main()
{
Customer C1 = new Customer()
{
ID = 101,
Name = "Mark",
Salary = 5000
};
Customer C2 = new Customer()
{
ID = 102,
Name = "Pam",
Salary = 6500
};
Customer C3 = new Customer()
{
ID = 119,
Name = "John",
Salary = 3500
};
////must get IndexOutOfRangeException as of accessing out of range
//Customer[] customers = new Customer[2];
//customers[0] = C1;
//customers[1] = C2;
//customers[2] = C3;
//doesn't get any exception as of list grows automatically
//strongly-typed class
List<Customer> customers = new List<Customer>(2);
customers.Add(C1);
customers.Add(C2);
customers.Add(C3);
//Customer c = customers[0]; //retrive first item
//Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
//foreach (Customer c in customers)
//{
// Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
//}
//getting out of rangeException
//for (int i = 0; i <= customers.Count; i++)
//{
// Customer c = customers[i];
// Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
//}
SavingsCustomer sc = new SavingsCustomer();
customers.Add(sc);
customers.Insert(0, C3); //<-- indexing gone down
foreach (Customer c in customers)
{
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
}
Console.WriteLine(customers.IndexOf(C3, 1,2)); //-1: item doen't find
if (customers.Contains(C3))
{
Console.WriteLine("C3 object exists in the list");
}
else
{
Console.WriteLine("C3 object does not exist in the list");
}
if (customers.Exists(cust => cust.Name.StartsWith("P")))
{
Console.WriteLine("customer exists in the list");
}
else
{
Console.WriteLine("customer does not exist in the list");
}
Customer c = customers.Find(cust => cust.Salary > 5000);
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
Customer c = customers.FindLast(cust => cust.Salary > 5000);
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
List<Customer> Customer = customers.FindAll(cust => cust.Salary > 5000);
foreach (Customer c in customers)
{
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
}
int index = customers.FindIndex(2, cust => cust.Salary > 5000);
Console.WriteLine("Index = " + index);
int index2 = customers.FindLastIndex(2, cust => cust.Salary > 5000);
Console.WriteLine("Index = " + index2);
// convert array to list
Customer[] customerArray = new Customer[3];
customerArray[0] = C1;
customerArray[1] = C2;
customerArray[2] = C3;
//ToList <- System.Linq namespace
List<Customer> listCustomer = customerArray.ToList();
foreach (Customer c in listCustomer)
{
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
}
//convert list to array
List<Customer> listCustomer = new List<Customer>();
listCustomer.Add(C1);
listCustomer.Add(C2);
listCustomer.Add(C3);
Customer[] customerArray= listCustomer.ToArray();
foreach (Customer c in customerArray)
{
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
}
//convert list to dictionary
List<Customer> listCustomer = new List<Customer>();
listCustomer.Add(C1);
listCustomer.Add(C2);
listCustomer.Add(C3);
Dictionary<int, Customer> dictionary = listCustomer.ToDictionary(x => x.ID);
foreach (KeyValuePair<int,Customer> kvp in dictionary)
{
Console.WriteLine("Key = " + kvp.Key);
Customer c = kvp.Value;
Console.WriteLine("ID = {0}, Name ={1}, Salary = {2}", c.ID, c.Name, c.Salary);
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
public class SavingsCustomer : Customer
{
}
}
=============================================
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Customer C1 = new Customer()
{
ID = 101,
Name = "Mark",
Salary = 5000
};
Customer C2 = new Customer()
{
ID = 102,
Name = "Pam",
Salary = 6500
};
Customer C3 = new Customer()
{
ID = 119,
Name = "John",
Salary = 3500
};
List<Customer> listCustomer = new List<Customer>();
listCustomer.Add(C1);
listCustomer.Add(C2);
listCustomer.Add(C3);
Console.WriteLine("Before Sorting");
foreach (Customer c in listCustomer)
{
Console.WriteLine(c.Salary);
}
listCustomer.Sort();
listCustomer.Reverse();
Console.WriteLine("After Sorting");
foreach (Customer c in listCustomer)
{
Console.WriteLine(c.Salary);
}
SortByName sortByName = new SortByName();
listCustomer.Sort(sortByName);
}
public class SortByName : IComparer<Customer>
{
public int CompareTo(Customer x, Customer y)
{
return x.Name.CompareTo(y.Name);
}
}
public class Customer : IComparable<Customer>
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int CompareTo(Customer other)
{
//if (this.Salary > other.Salary)
// return 1;
//else if (this.Salary < other.Salary)
// return -1;
//else
// return 0;
return this.Salary.CompareTo(other.Salary);
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Customer C1 = new Customer()
{
ID = 101,
Name = "Mark",
Salary = 5000
};
Customer C2 = new Customer()
{
ID = 105,
Name = "Pam",
Salary = 6500
};
Customer C3 = new Customer()
{
ID = 102,
Name = "John",
Salary = 3500
};
List<Customer> listCustomer = new List<Customer>();
listCustomer.Add(C1);
listCustomer.Add(C2);
listCustomer.Add(C3);
/* Sorting a list of complex types
* Sort() - List class expects Comparison delegate to be passed as an argument
* 1. Create a function whose signature matches the signature of System.Comparison delegate
* This is the method where we need to write the logic to compare 2 customer objects.
* 2. Create an instance of System.Comparison delegate, and then pass the name of the function created in step 1 as the argument.
* so at this point "Comparison" delegate is pointing to the function that contains the logic to compare 2 customer objects.
* 3. Pass the delegate instance as an argument, to Sort() function
* */
//2.
//Comparison<Customer> customerComparer = new Comparison<Customer>(CompareCustomer);
Console.WriteLine("Before Sorting");
foreach (Customer c in listCustomer)
{
Console.WriteLine(c.ID);
}
//3.
//listCustomer.Sort(customerComparer); <--following 3 steps
//listCustomer.Sort(delegate(Customer c1, Customer c2) { return c1.ID.CompareTo(C2.ID); }); //using delegate keyword
listCustomer.Sort((x,y) => x.ID.CompareTo(y.ID)); //using lambda expression
Console.WriteLine("After Sorting");
foreach (Customer c in listCustomer)
{
Console.WriteLine(c.ID);
}
}
////1. should match with Comparison
//private static int CompareCustomer(Customer x, Customer y)
//{
// return x.ID.CompareTo(y.ID);
//}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
=================================
useful functions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
class Program
{
static void Main()
{
Customer C1 = new Customer()
{
ID = 101,
Name = "Mark",
Salary = 5000
};
Customer C2 = new Customer()
{
ID = 105,
Name = "Pam",
Salary = 6500
};
Customer C3 = new Customer()
{
ID = 102,
Name = "John",
Salary = 3500
};
List<Customer> listCustomer = new List<Customer>();
listCustomer.Add(C1);
listCustomer.Add(C2);
listCustomer.Add(C3);
//can't add, delete
ReadOnlyCollection<Customer> readonlyCustomers = listCustomer.AsReadOnly(); //using System.Collections.ObjectModel;
Console.WriteLine("Items = " + readonlyCustomers.Count);
Console.WriteLine("Are all salaries greater than 3000 = " + listCustomer.TrueForAll(x => x.Salary > 3000));
Console.WriteLine("Capacity before trimming = " + listCustomer.Capacity);
listCustomer.TrimExcess(); //useful but need to cost
Console.WriteLine("Capacity after trimming = " + listCustomer.Capacity);
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}