List Collection class in C#
List is one of the generic collection classes present in system.collection.generic namespace.
There are several generic collection classes in system.collection.generic namespace as listed below.
1. Dictionary
2.List
3.Stack
4.Queue etc
A List class can be used to create a collection of any type.
For example, we can create a list of integers,strings and even complex types.
The objects stored in the list can be accessed by index.
This class also provides methods to search, sort, and manipulate lists.
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = "Malla",
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = "Mark",
Salary = 5000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = "Mla",
Salary = 5000
};
List<Customer> customers = new List<Customer>(2);
customers.Add(Customer1);
customers.Add(Customer2);
customers.Add(Customer3);
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);
Console.ReadLine();
}
//foreach (Customer c in customers)
//{
// Console.WriteLine("ID = {0}, Name = {1},Salary = {2}", c.ID, c.Name, c.Salary);
// Console.ReadLine();
//}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}
=====================================================================
customers.Insert(0, Customer3);
foreach(Customer c in customers)
{
Console.WriteLine(c.ID);
Console.ReadLine();
}
====================================================================
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = "Malla",
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = "Mark",
Salary = 5000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = "Mla",
Salary = 5000
};
List<Customer> customers = new List<Customer>(2);
customers.Add(Customer1);
customers.Add(Customer2);
customers.Add(Customer3);
customers.Insert(0, Customer3);
Console.WriteLine(customers.IndexOf(Customer3,1,3));
Console.ReadLine();
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}
=======================================================================
1. Contains() function- Checks if an item exists in the list.This method returns true if the item exists, else false.
2.Exists() function-Checks if an item exists in the list based on a condition. This method returns true if the items exists, else false.
3.Find() function - Searches for an element that matches the conditions defined by the specified lambda expression and returns the first matching item from the list.
4. FindLast() function-Searches for an element that matches the conditions defined by the specified lambda expression and returns the last matching item from the list.
5. FindAll() function-Returns all the items from the list that match the conditions specified by the lambda expression.
6.FindIndex() function- Returns the index of the first item, that matches the condition specified by the lambda expression. There are 2 other overloads of this method which allows us to specify the range of elements to search, with in the list.
7.FindLastIndex() function-Returns the index of the last item, that matches the condition specified by the lambda expression. There are 2 other overloads of this method which allows us to specify the range of elements to search, with in the list.
Convert an array to a List - Use ToList() method
Convert a list to an array - Use ToArray() method
Convert a List to a Dictionary - Use ToDictionary() method
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = "Malla",
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = "Mark",
Salary = 5000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = "Mla",
Salary = 5000
};
List<Customer> listcustomers = new List<Customer>(2);
listcustomers.Add(Customer1);
listcustomers.Add(Customer2);
if(listcustomers.Contains(Customer3))
{
Console.WriteLine("Customers3 object exists in the lsit");
Console.ReadLine();
}
else
{
Console.WriteLine("Customer3 object does not exist in the list");
Console.ReadLine();
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}
=====================================================================
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = "Malla",
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = "Mark",
Salary = 5000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = "Mla",
Salary = 5000
};
List<Customer> listcustomers = new List<Customer>(2);
listcustomers.Add(Customer1);
listcustomers.Add(Customer2);
listcustomers.Add(Customer3);
if(listcustomers.Exists(cust => cust.Name.StartsWith("Z")))
{
Console.WriteLine("Customers3 object exists in the list");
Console.ReadLine();
}
else
{
Console.WriteLine("Customer3 object does not exist in the list");
Console.ReadLine();
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}
---------------------------------------------------------------------------------------------------------------------
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = "Malla",
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = "Mark",
Salary = 7000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = "Mla",
Salary = 5500
};
List<Customer> listcustomers = new List<Customer>(2);
listcustomers.Add(Customer1);
listcustomers.Add(Customer2);
listcustomers.Add(Customer3);
Customer c = listcustomers.Find(cust => cust.Salary > 5000);
// Customer c = listcustomers.FindLast(cust => cust.Salary > 5000);
Console.WriteLine("ID={0},Name = {1},Salary = {2}", c.ID, c.Name, c.Salary);
Console.ReadLine();
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}
=======================================================================
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = "Malla",
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = "Mark",
Salary = 7000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = "Mla",
Salary = 5500
};
List<Customer> listcustomers = new List<Customer>(2);
listcustomers.Add(Customer1);
listcustomers.Add(Customer2);
listcustomers.Add(Customer3);
List<Customer> customers = listcustomers.FindAll(cust => cust.Salary > 5000);
foreach (Customer c in customers)
{
Console.WriteLine("ID={0},Name = {1},Salary = {2}", c.ID, c.Name, c.Salary);
Console.ReadLine();
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}