Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Remove range of items from list by passing start index and number of elements to delete

C# Sharp LINQ : Exercise-21 with Solution

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.

Sample Solution:-

C# Sharp Code:

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

class  LinqExercise21
    {  
        static void Main(string[] args)  
      {  
           
            List<string> listOfString = new List<string>();  
            listOfString.Add("m");  
            listOfString.Add("n");  
            listOfString.Add("o");  
            listOfString.Add("p");  
            listOfString.Add("q");  
			
			
            Console.Write("\nLINQ : Remove range of items from list by passing start index and number of elements to delete  : "); 
            Console.Write("\n------------------------------------------------------------------------------------------------\n");
            
            var _result1 = from y in listOfString  
            select y; 
            Console.Write("Here is the list of items : \n");
            foreach(var tchar in _result1)  
            {  
                Console.WriteLine("Char: {0} ", tchar);  
            } 

            listOfString.RemoveRange(1, 3);
  
            var _result = from z in listOfString  
            select z;  
  	        Console.Write("\nHere is the list after removing the three items starting from the item index 1 from the list : \n");
            foreach(var rChar in _result)  
            {  
                Console.WriteLine("Char: {0} ", rChar);  
            }  
  
            Console.ReadLine();  
        }  
    }
	

Sample Output:

LINQ : Remove range of items from list by passing start index and number of elements to delete  :             
------------------------------------------------------------------------------------------------              
Here is the list of items :                                                                                   
Char: m                                                                                                     
Char: n                                                                                                     
Char: o                                                                                                     
Char: p                                                                                                     
Char: q                                                                                                     
Here is the list after removing the three items starting from the item index 1 from the list :                
Char: m                                                                                                     
Char: q

Pictorial Presentation:

C# Sharp LINQ: Remove range of items from list by passing start index and number of elements to delete.

Flowchart:

Flowchart: LINQ : Remove range of items from list by passing start index and number of elements to delete

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 the item index.
Next: Write a program in C# Sharp to find the strings for a specific minimum length.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.