Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Remove all elements from the SortedSet
Next article icon

C# | Remove all elements from a SortedList

Last Updated : 01 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Clear method is used to remove all the elements from a SortedList object. Properties:
  • A SortedList element can be accessed by its key or by its index.
  • A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values.
  • A key cannot be null, but a value can be.
  • The capacity of a SortedList object is the number of elements the SortedList can hold.
  • A SortedList does not allow duplicate keys.
  • Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
Syntax :
  public virtual void Clear ();  
Exceptions:
  • NotSupportedException : If the SortedList object is read-only or has a fixed size.
Example: CSHARP
// C# code to remove all // elements from a SortedList using System; using System.Collections;  class GFG {      // Driver code     public static void Main()     {          // Creating an SortedList         SortedList mySortedList = new SortedList();          // Adding elements to SortedList         mySortedList.Add("1", "1st");         mySortedList.Add("2", "2nd");         mySortedList.Add("3", "3rd");         mySortedList.Add("4", "4th");         mySortedList.Add("5", "5th");         mySortedList.Add("6", "6th");         mySortedList.Add("7", "7th");          // Displaying number of elements         Console.WriteLine("Number of elements in SortedList is : "                                              + mySortedList.Count);          // Displaying capacity         Console.WriteLine("capacity of SortedList is : "                                 + mySortedList.Capacity);          // Removing all elements from SortedList         mySortedList.Clear();          // Displaying number of elements         Console.WriteLine("Number of elements in SortedList is : "                                             + mySortedList.Count);          // Displaying capacity         Console.WriteLine("capacity of SortedList is : "                                 + mySortedList.Capacity);     } } 
Output:
  Number of elements in SortedList is : 7  capacity of SortedList is : 16  Number of elements in SortedList is : 0  capacity of SortedList is : 16  
Note:
  • This method is an O(n) operation, where n is Count.
  • Count is set to zero and references to other objects from elements of the collection are also released.
  • Capacity remains unchanged. To reset the capacity of the SortedList object, call TrimToSize or set the Capacity property directly.
  • Trimming an empty SortedList sets the capacity of the SortedList to the default capacity.
Reference:
  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.clear?view=netframework-4.7.2

Next Article
C# | Remove all elements from the SortedSet

S

Sahil_Bansall
Improve
Article Tags :
  • Python
  • C#
  • CSharp-method
  • CSharp-Collections-Namespace
  • CSharp-Collections-SortedList
Practice Tags :
  • python

Similar Reads

  • C# | Remove all elements from the SortedSet
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Clear Method is used to remove the all elements from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elements.
    2 min read
  • C# | Remove all elements from the ArrayList
    ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis
    3 min read
  • 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. L
    3 min read
  • C# | Remove a range of elements from the ArrayList
    ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveRange(Int32, Int32) method is used to remove a range of ele
    3 min read
  • C# | Remove the element with the specified key from a SortedList
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Remove(Object) method is used to remove the element with the specifi
    3 min read
  • C# | Remove elements from a SortedSet that match the predicate
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.RemoveWhere(Predicate<T>) Method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet<T
    3 min read
  • C# | Remove all elements from the Collection<T>
    Collection<T>.Clear method is used to remove all elements from the Collection<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: // C# code to remove all // elements from the Collection using System; using System.C
    2 min read
  • C# | Remove all elements from OrderedDictionary
    OrderedDictionary.Clear method is used to remove all elements from the OrderedDictionary collection. Syntax: public void Clear (); Exception: If the OrderedDictionary collection is read-only then it will throw NotSupportedException. Below given are some examples to understand the implementation in a
    2 min read
  • C# | Copying the SortedList elements to an Array Object
    SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat
    2 min read
  • C# | Add element to SortedSet
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Add(T) Method is used to add an element to the set and returns a value that specify if it was successfully added or not. Properties: In C#, SortedSet class
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences