Implementation of C# List class: Add() and Insert() methods
Program.cs
using System.Collections.Generic;

List<string> li = new List<string>();
li.Add("Apple");
li.Add("Orange");
li.Add("Mango");
li.Add("Grapes");
li.Add("Cherry");
li.Add("Apple");

li.Insert(0, "Pineapple");

for (int i = 0; i < li.Count; i++)
{
    Console.Write(li[i] + " ");
}
Console.WriteLine("\nSize: " + li.Count);
Console.WriteLine("Capacity: " + li.Capacity);
Output
kodingwindow@kw:~/csharp$ dotnet run
Pineapple Apple Orange Mango Grapes Cherry Apple 
Size: 7
Capacity: 8
kodingwindow@kw:~/csharp$ 
Implementation of C# List class: Sort() and Reverse() methods
Program.cs
using System.Collections.Generic;

List<string> li = new List<string>();
li.Add("Apple");
li.Add("Orange");
li.Add("Mango");
li.Add("Grapes");
li.Add("Cherry");
li.Add("Apple");

li.Sort();
for (int i = 0; i < li.Count; i++)
{
    Console.Write(li[i] + " ");
}

Console.WriteLine();
li.Reverse();
for (int i = 0; i < li.Count; i++)
{
    Console.Write(li[i] + " ");
}

Console.WriteLine();
li.Reverse(2, 4); //li.Reverse(int index, int count)
for (int i = 0; i < li.Count; i++)
{
    Console.Write(li[i] + " ");
}
Console.WriteLine();
Output
kodingwindow@kw:~/csharp$ dotnet run
Apple Apple Cherry Grapes Mango Orange 
Orange Mango Grapes Cherry Apple Apple 
Orange Mango Apple Apple Cherry Grapes 
kodingwindow@kw:~/csharp$ 
Implementation of C# List class: Remove(), RemoveAt(), and Clear() methods
Program.cs
using System.Collections.Generic;

List<string> li = new List<string>();
li.Add("Apple");
li.Add("Orange");
li.Add("Mango");
li.Add("Grapes");
li.Add("Cherry");
li.Add("Apple");

li.Remove("Apple");
li.RemoveAt(0);
for (int i = 0; i < li.Count; i++)
{
    Console.Write(li[i] + " ");
}

li.Clear();
if (li.Count == 0)
{
    Console.WriteLine("\nList is empty now");
}
Output
kodingwindow@kw:~/csharp$ dotnet run
Mango Grapes Cherry Apple 
List is empty now
kodingwindow@kw:~/csharp$ 
Implementation of C# List class: IndexOf() and LastIndexOf() methods
Program.cs
using System.Collections.Generic;

List<string> li = new List<string>();
li.Add("Apple");
li.Add("Orange");
li.Add("Mango");
li.Add("Grapes");
li.Add("Cherry");
li.Add("Apple");
li.Add("Apple");

Console.WriteLine("First match at: " + li.IndexOf("Apple"));
Console.WriteLine("Last match at: " + li.LastIndexOf("Apple"));
Output
kodingwindow@kw:~/csharp$ dotnet run
First match at: 0
Last match at: 6
kodingwindow@kw:~/csharp$ 
Implementation of C# List class: IndexOf() and LastIndexOf() methods
Program.cs
using System.Collections.Generic;

List<string> li1 = new List<string>();
li1.Add("Apple");
li1.Add("Orange");
li1.Add("Mango");

List<string> li2 = new List<string>();
li2.Add("Grapes");
li2.Add("Cherry");
li2.Add("Apple");

li1.AddRange(li2);
for (int i = 0; i < li1.Count; i++)
{
    Console.Write(li1[i] + " ");
}
Console.WriteLine();
Output
kodingwindow@kw:~/csharp$ dotnet run
Apple Orange Mango Grapes Cherry Apple 
kodingwindow@kw:~/csharp$ 
Advertisement