📜  C#|获取或设置与SortedList中的指定键关联的值

📅  最后修改于: 2021-05-29 15:34:53             🧑  作者: Mango

SortedList.Item [Object]属性用于获取和设置与SortedList对象中的特定键关联的值。

句法:

public virtual object this[object key] { get; set; }

在此,键与要获取或设置的值相关联。它是对象类型。

返回值:如果找到键,则此属性返回与SortedList对象中的key参数关联的值,否则返回null。

例外情况:

  • ArgumentNullException:如果键为null。
  • NotSupportedException:如果设置了属性并且SortedList对象是只读的,或者如果设置了属性,则键在集合中不存在,并且SortedList具有固定的大小。
  • OutOfMemoryException:如果没有足够的可用内存将元素添加到SortedList。
  • InvalidOperationException:如果比较器引发异常。

下面的程序说明了上面讨论的属性的用法:

范例1:

// C# code to Gets or sets the value 
// associated with the specified key 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating a SortedList 
        SortedList mylist = new SortedList(); 
  
        // Adding elements in SortedList 
        mylist.Add("g", "geeks"); 
        mylist.Add("c", "c++"); 
        mylist.Add("d", "data structures"); 
        mylist.Add("q", "quiz"); 
  
        // Get a collection of the keys.
        ICollection c = mylist.Keys; 
  
        // Displaying the contents 
        foreach(string str in c) 
            Console.WriteLine(str + ": " + mylist[str]); 
  
        // Setting the value associated with key "c" 
        mylist["c"] = "C#"; 
  
        Console.WriteLine("Updated Values:"); 
  
        // Displaying the contents 
        foreach(string str in c) 
            Console.WriteLine(str + ": " + mylist[str]); 
    } 
} 
输出:
c: c++
d: data structures
g: geeks
q: quiz
Updated Values:
c: C#
d: data structures
g: geeks
q: quiz

范例2:

// C# code to Gets or sets the value 
// associated with the specified key 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating a SortedList 
        SortedList mylist = new SortedList(); 
  
        // Adding elements in SortedList 
        mylist.Add("4", "Even"); 
        mylist.Add("9", "Odd"); 
        mylist.Add("5", "Odd and Prime"); 
        mylist.Add("2", "Even and Prime"); 
  
        // Get a collection of the keys.
        ICollection c = mylist.Keys; 
  
        // Displaying the contents 
        foreach(string str in c) 
            Console.WriteLine(str + ": " + mylist[str]); 
  
        // Setting the value associated 
        // with key "56" which is not present  
        // will result in the creation of  
        // new key and value will be set which  
        // is given by the user 
        mylist["56"] = "New Value"; 
    
        Console.WriteLine("Updated Values:"); 
    
        // Displaying the contents 
        foreach(string str in c) 
            Console.WriteLine(str + ": " + mylist[str]); 
              
              
              
        // Setting the value associated 
        // with key "28" which is not present  
        // will result in the creation of  
        // new key and its value can be null
        mylist["28"] = null; 
    
        Console.WriteLine("Updated Values:"); 
    
        // Displaying the contents 
        foreach(string str in c) 
            Console.WriteLine(str + ": " + mylist[str]); 
    } 
} 
输出:
2: Even and Prime
4: Even
5: Odd and Prime
9: Odd
Updated Values:
2: Even and Prime
4: Even
5: Odd and Prime
56: New Value
9: Odd
Updated Values:
2: Even and Prime
28: 
4: Even
5: Odd and Prime
56: New Value
9: Odd

笔记:

  • 此属性返回与特定键关联的值。如果未找到该键,而一个人试图获取该键,则此属性将返回null;如果尝试设置该属性,则将导致使用指定的键创建一个新元素。
  • 键不能为null,但值可以为null。若要区分由于找不到指定键而返回的null和由于指定键的值为null而返回的null,请使用Contains方法或ContainsKey方法确定该键是否在列表中。
  • 检索此属性的值是O(log n)操作,其中n是Count。如果键已在SortedList中,则设置属性为O(log n)操作。如果键不在列表中,则设置属性为对未排序数据进行O(n)操作;如果在列表末尾添加新元素,则设置为O(log n)。如果插入导致调整大小,则该操作为O(n)。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist.item?view=netframework-4.7.2