w3resource

C#: Remove items from list by passing the item index

C# Sharp LINQ : Exercise-20 with Solution

Write a program in C# Sharp to remove items from list by passing the item index.

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class LinqExercise20
{
    static void Main(string[] args)
    {
        // Initialize a list of strings
        List<string> listOfString = new List<string>();
        listOfString.Add("m");
        listOfString.Add("n");
        listOfString.Add("o");
        listOfString.Add("p");
        listOfString.Add("q");

        // Display a message indicating the purpose of the following output
        Console.Write("\nLINQ : Remove items from list by passing item index  : ");
        Console.Write("\n--------------------------------------------------\n");

        // Using LINQ to select all items from the list
        var _result1 = from y in listOfString
                       select y;

        // Display the initial list items
        Console.Write("Here is the list of items : \n");
        foreach (var tchar in _result1)
        {
            Console.WriteLine("Char: {0} ", tchar);
        }

        // Remove an item at index 3 from the list
        listOfString.RemoveAt(3);

        // Using LINQ to select all items after removal of an item at index 3
        var _result = from z in listOfString
                      select z;
        Console.Write("\nHere is the list after removing item index 3 from the list : \n");

        // Display the updated list items
        foreach (var rChar in _result)
        {
            Console.WriteLine("Char: {0} ", rChar);
        }

        Console.ReadLine(); // Wait for user input before exiting
    }
}

Sample Output:

LINQ : Remove items from list by passing item index  :                                                        
--------------------------------------------------                                                            
Here is the list of items :                                                                                   
Char: m                                                                                                     
Char: n                                                                                                     
Char: o                                                                                                     
Char: p                                                                                                     
Char: q                                                                                                     
Here is the list after removing item index 3 from the list :                                                  
Char: m                                                                                                     
Char: n                                                                                                     
Char: o                                                                                                     
Char: q

Visual Presentation:

C# Sharp LINQ: Remove Items from List by passing the item index.

Flowchart:

Flowchart: LINQ : Remove Items from List by passing item index

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to Remove Items from List by passing filters.
Next: Write a program in C# Sharp to remove a range of items from a list by passing the start index and number of elements to remove.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/linq/csharp-linq-exercise-20.php