Open In App

C# | Removing all the elements from the List

List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. List.Clear Method is used remove the all the elements from the List.

Properties:



Syntax:

public void Clear ();

Below programs illustrate how to remove all the elements from the List:



Example 1:




// C# program to remove all the
// elements from a List
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // Creating a List of integers
        List<int> list1 = new List<int>();
  
        // Inserting the elements into the List
        list1.Add(1);
        list1.Add(4);
        list1.Add(3);
        list1.Add(1);
        list1.Add(2);
  
        // Displaying the count of elements
        // contained in the List before
        // removing all the elements
        Console.Write("Number of elements in the List Before Removing: ");
  
        // using Count property
        Console.WriteLine(list1.Count);
  
        // Removing all elements from list
        list1.Clear();
  
        // Displaying the count of elements
        // contained in the List after
        // removing all the elements
        Console.Write("Number of elements in the List After Removing: ");
  
        // using Count property
        Console.WriteLine(list1.Count);
    }
}

Output:

Number of elements in the List Before Removing: 5
Number of elements in the List After Removing: 0

Example 2:




// C# program to remove all the
// elements from a List
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // Creating a List of strings
        List<string> list1 = new List<string>();
  
        // Inserting the elements into the List
        list1.Add("Welcome");
        list1.Add("To");
        list1.Add("Geeks");
        list1.Add("for");
        list1.Add("Geeks");
        list1.Add("Geeks");
        list1.Add("Geeks");
  
        // Displaying the count of elements
        // contained in the List before
        // removing all the elements
        Console.Write("Number of elements in the List Before Removing: ");
  
        // using Count property
        Console.WriteLine(list1.Count);
  
        // Removing all elements from list
        list1.Clear();
  
        // Displaying the count of elements
        // contained in the List after
        // removing all the elements
        Console.Write("Number of elements in the List After Removing: ");
  
        // using Count property
        Console.WriteLine(list1.Count);
    }
}

Output:

Number of elements in the List Before Removing: 7
Number of elements in the List After Removing: 0

Reference:


Article Tags :
C#