c sharp

C# SortedLits.RemoveAt() Method

SortedList in C# stores the values linearly that holds the key-value pairs. If you want to store the key:value pair, using the SortedList is the best practice. In this tutorial, we will see how to remove the particular values in a sorted list using the RemoveAt() method. We can create a SortedList in C# using the following syntax:

SortedList object= new SortedList();

 
If you want to display the key and value, you can use the GetKey() and GetByIndex() methods.

RemoveAt() Method

The RemoveAt() method in SortedList removes the values based on the index number provided.

Syntax:

SortedList_object.RemoveAt(index_number);

 
Parameter:

It takes only one parameter that represents the index position in which the element is to be removed.

As we know, indexing starts with 0.

Example 1:

Here, we will create a SortedList that has 5 key values with string type and remove the particular elements.

using System;
using System.Collections;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create sorted list named - slist.
        SortedList slist = new SortedList();
       
        //add 5 values to the above slist.
        slist.Add("1","Popcorn");
        slist.Add("2","Fruits");
        slist.Add("3","Organic food");
        slist.Add("4","Vegtables");
        slist.Add("5","Sweets");
     
     //Display key and value from slist
     Console.WriteLine( "----------Actual Values----------" );
     for ( int value = 0; value < slist.Count; value++ )  {
         Console.WriteLine( slist.GetKey(value)+" -> "+slist.GetByIndex(value) );
      }
        //remove 5th element
         slist.RemoveAt(4);
         
         //remove 1st element
         slist.RemoveAt(0);
         
         //Display key and value from slist
     Console.WriteLine( "----------After removing 1st and 5th values----------" );
     for ( int value = 0; value < slist.Count; value++ )  {
         Console.WriteLine( slist.GetKey(value)+" -> "+slist.GetByIndex(value) );
     }
       
       
    }
           
}

 
Output:


Explanation:

1. We create a SortedList named slist.
 

2. Add five values to the slist.
 

3. Remove the values present at index 4 and 0.
 

4. Display the final SortedList.
 

Example 2:

Here, we will create a SortedList that has 5 key values with Integer type and remove the particular elements.

using System;
using System.Collections;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create sorted list named - slist.
        SortedList slist = new SortedList();
       
        //add 5 values to the above slist.
        slist.Add(1,100);
        slist.Add(2,200);
        slist.Add(3,300);
        slist.Add(4,400);
        slist.Add(5,500);
     
     //Display key and value from slist
     Console.WriteLine( "----------Actual Values----------" );
     for ( int value = 0; value < slist.Count; value++ )  {
         Console.WriteLine( slist.GetKey(value)+" -> "+slist.GetByIndex(value) );
      }
        //remove 2nd  element
         slist.RemoveAt(2);
         
         //remove 4th element
         slist.RemoveAt(3);
         
         //remove 1st element
         slist.RemoveAt(0);
         
         //Display key and value from slist
     Console.WriteLine( "----------After removing 1st,2nd and 4th values----------" );
     for ( int value = 0; value < slist.Count; value++ )  {
         Console.WriteLine( slist.GetKey(value)+" -> "+slist.GetByIndex(value) );
     }
       
       
    }
           
}

 
Output:

Explanation:

1. We create a SortedList named slist.
 

2. Add five values to the slist.
 

3. Remove the values present at index 2, 3 and 0.
 

4. Display the final SortedList.
 

Conclusion

We can remove the elements based on the index number using the RemoveAt() method in the SortedList. We demonstrated two different examples with different data type values to understand the concept better. Make sure to use the modules using System, using System.Collections in your code.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain