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# | Getting the keys in a SortedList object
Next article icon

C# | Search in a SortedList object

Last Updated : 11 Oct, 2018
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.ContainsKey(Object) method is used to check whether a SortedList object contains a specific key or not.

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 bool ContainsKey (object key);  

Here, key is the key to locate in the SortedList object.

Return Value: This method will return True if the SortedList object contains an element with the specified key otherwise it returns False.

Exceptions:

  • ArgumentNullException : If the key is null.
  • InvalidOperationException : If the comparer throws an exception.

Below given are some examples to understand the implementation in a better way:

Example 1:




// C# code to check if a SortedList
// object contains a specific key
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("h", "Hello");
        mySortedList.Add("g", "Geeks");
        mySortedList.Add("f", "For");
        mySortedList.Add("n", "Noida");
  
        // Checking if a SortedList object
        // contains a specific key
        Console.WriteLine(mySortedList.ContainsKey("g"));
    }
}
 
 
Output:
  True  

Example 2:




// C# code to check if a SortedList
// object contains a specific key
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("h", "Hello");
        mySortedList.Add("g", "Geeks");
        mySortedList.Add("f", "For");
        mySortedList.Add("n", "Noida");
  
        // Checking if a SortedList object
        // contains a specific key
        // It should throw ArgumentNullException
        // as the Key can not be null
        Console.WriteLine(mySortedList.ContainsKey(null));
    }
}
 
 

Error:

Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key

Note: This method uses a binary search algorithm, therefore, this method is an O(log n) operation, where n is Count.

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.containskey?view=netframework-4.7.2


Next Article
C# | Getting the keys in a SortedList object

S

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

Similar Reads

  • C# | Check if a SortedList object has a fixed size
    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.IsFixedSize property is used to check whether a SortedList object ha
    2 min read
  • C# | Getting the keys in a SortedList object
    SortedList.Keys Property is used to get the keys in a SortedList object. Syntax: public virtual System.Collections.ICollection Keys { get; } Property Value: An ICollection object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed property: Example 1: /
    2 min read
  • C# | Check if two SortedList objects are equal
    Equals(Object) Method which is inherited from the Object class is used to check whether the specified SortedList object is equal to another SortedList object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Value
    2 min read
  • C# | Getting the Values in a SortedList object
    SortedList.Values Property is used to get the values in a SortedList object. Syntax: public virtual System.Collections.ICollection Values { get; } Property Value: An ICollection object containing the values in the SortedList object. Below programs illustrate the use of above-discussed property: Exam
    2 min read
  • C# | How to create 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. Properties of SortedList: Internally the object of SortedList maintains two arr
    2 min read
  • C# | Check if a SortedList is read-only
    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.IsReadOnly property is used to get a value which indicates that a So
    2 min read
  • C# | Getting the list of keys of a SortedList object
    SortedList.GetKeyList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetKeyList (); Return Value: It returns an IList object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed method: Exam
    2 min read
  • C# | Getting the list of Values of a SortedList object
    SortedList.GetValueList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetValueList (); Return Value: It returns an IList object containing the values in the SortedList object. Below programs illustrate the use of above-discussed method
    2 min read
  • C# SortedList Class
    SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic
    6 min read
  • C# | How to add key/value pairs in 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.Add(Object, Object) Method is used to add an element with the specif
    3 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