
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Remove all elements from a SortedList in C#
To remove all elements from a SortedList, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(String[] args){ SortedList sortedList = new SortedList(); sortedList.Add("A", "1"); sortedList.Add("B", "2"); sortedList.Add("C", "3"); sortedList.Add("D", "4"); sortedList.Add("E", "5"); sortedList.Add("F", "6"); sortedList.Add("G", "7"); sortedList.Add("H", "8"); sortedList.Add("I", "9"); sortedList.Add("J", "10"); Console.WriteLine("SortedList elements..."); foreach(DictionaryEntry d in sortedList){ Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count); sortedList.Clear(); Console.WriteLine("Count of SortedList (updated) = "+sortedList.Count); } }
Output
This will produce the following output −
SortedList elements... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 Count of SortedList key-value pairs = 10 Count of SortedList (updated) = 0
Example
Let us see another example −
using System; using System.Collections; public class Demo { public static void Main(String[] args){ SortedList sortedList = new SortedList(); sortedList.Add("A", "1"); sortedList.Add("B", "2"); sortedList.Add("C", "3"); Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count); Console.WriteLine("SortedList elements..."); foreach(DictionaryEntry d in sortedList){ Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } sortedList.Clear(); Console.WriteLine("Count of SortedList key-value pairs (updated) = "+sortedList.Count); } }
Output
This will produce the following output −
Count of SortedList key-value pairs = 3 SortedList elements... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Count of SortedList key-value pairs (updated) = 0
- Related Articles
- Remove all elements from a HashSet in C#
- Remove all elements from OrderedDictionary in C#
- Remove from the specified index of a SortedList in C#
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- Remove all elements in a collection from a HashSet in C#
- Remove all elements from a HashSet in Java
- Remove the element with the specified key from a SortedList in C#
- Remove all elements from TreeSet in Java
- Remove all elements from Java NavigableMap
- Remove all elements from Java LinkedHashSet
- Remove all elements from the ArrayList in Java
- Java Program to remove all elements from a set in Java

Advertisements