📜  C# 中的 SortedList ContainsKey() 方法及示例

📅  最后修改于: 2022-05-13 01:55:39.168000             🧑  作者: Mango

C# 中的 SortedList ContainsKey() 方法及示例

给定一个 SortedList 对象,现在我们的任务是检查给定的 SortedList 对象是否包含特定的键。所以为了完成这个任务,我们使用ContainsKey()方法。此方法用于确定 SortedList 对象是否包含特定键。如果找到密钥,它将返回 true,否则,它将返回 false。

句法:

其中 key 位于 SortedList 对象中。

例子:

Input  : [(1, "Python"), (2, "c")]
Key    : 1
Output : Found
Input  : [(1, "Python"), (2, "c")]
Key    : 4
Output : Not Found

方法:

C#
// C# program to check whether a SortedList 
// object contains a specific key or not
using System;
using System.Collections;
  
class GFG{
      
static public void Main()
{
      
    // Create sorted list
    SortedList data = new SortedList();
      
    // Add elements to data sorted list 
    data.Add(1, "Python");
    data.Add(2, "c");
    data.Add(3, "java");
    data.Add(4, "php");
    data.Add(5, "html");
    data.Add(6, "bigdata");
    data.Add(7, "java script");
      
    // Check whether the key - 1 is present
    // in the list or not
    if (data.ContainsKey(1))
        Console.WriteLine("Present in the List");
    else
        Console.WriteLine("Not in the List");
          
    // Check whether the key - 4 is present
    // in the list or not
    if (data.ContainsKey(4))
        Console.WriteLine("Present in the List");
    else
        Console.WriteLine("Not in the List");
      
    // Check whether the key - 8 is present
    // in the list or not
    if (data.ContainsKey(8))
        Console.WriteLine("Present in the List");
    else
        Console.WriteLine("Not in the List");
}
}


输出:

Present in the List
Present in the List
Not in the List