A List class can be used to create a collection of elements of any data type. We can create list collection like integer type,string type etc.. Objects stored in the list can be accessed by index. List allows duplicate values to be inserted.
List class derived from System.Collections.Generic.
List size can grow automatically based on the elements supplied. List class also supports different functionalities like search,sort,delete etc...
Syntax:
List<Datatype> <Variable name> = new List<Datatype>();
Example:
List<int> list = new List<int>();
using System;
using System.Collections.Generic;
class CollectionList
{
public void ListMethod()
{
List list = new List();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
int[] a = new int[5] { 6, 7, 8, 9, 10 };
list.AddRange(a);
list.Sort();
list.Reverse();
list.Reverse();
list.Remove(5);
list.RemoveAt(7);
list.RemoveRange(5, 2);
foreach (var item in list)
{
Console.WriteLine("List item value is : " + item);
}
list.RemoveRange(3, 2);
foreach (var item in list)
{
Console.WriteLine("List item value is : " + item);
}
}
}
public class Program
{
public static void Main()
{
CollectionList c = new CollectionList();
c.ListMethod();
}
}
Practice




SQL Server
C#.Net
ASP.Net
ADO.Net
jQuery
No comments:
Post a Comment