📌  相关文章
📜  C#|获取或设置StringDictionary中指定键处的值

📅  最后修改于: 2021-05-29 20:30:45             🧑  作者: Mango

StringDictionary.Item [String]属性用于获取或设置与指定键关联的值。

句法:

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

在这里, key是System.String类型的Key,其值是get或set。

返回值:该属性返回与指定键关联的值。如果找不到指定的键,则Get返回null ,而Set使用指定的键创建一个新条目。

异常:如果为null,则此属性将引发ArgumentNullException

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

范例1:

// C# code to get or set the value at
// the specified key in StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named my1
        StringDictionary my1 = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        my1.Add("1", "C");
        my1.Add("2", "C++");
        my1.Add("3", "Java");
        my1.Add("4", "Python");
        my1.Add("5", "C#");
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d in my1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
  
        Console.WriteLine("\nAfter Item[String] Property: \n");
  
        // setting the value at key 2
        my1["2"] = "HTML";
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d1 in my1)
        {
            Console.WriteLine(d1.Key + " " + d1.Value);
        }
    }
}

输出:

3 Java
5 C#
4 Python
2 C++
1 C

After Item[String] Property: 

3 Java
4 Python
2 HTML
1 C
5 C#

范例2:

// C# code to get or set the value at
// the specified key in StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named my1
        StringDictionary my1 = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        my1.Add("1", "HTML");
        my1.Add("2", "CSS");
        my1.Add("3", "PHP");
        my1.Add("4", "MongoDB");
        my1.Add("5", "AngularJS");
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d in my1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
  
        Console.WriteLine("\nAfter Item[String] Property: \n");
  
        // setting the value at Key 8
        // here key 8 is not present
        // so it will add a new key/value
        // pair. see output
        my1["8"] = "C#";
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d1 in my1)
        {
            Console.WriteLine(d1.Key + " " + d1.Value);
        }
    }
}

输出:

3 PHP
5 AngularJS
4 MongoDB
2 CSS
1 HTML

After Item[String] Property: 

3 PHP
4 MongoDB
2 CSS
1 HTML
8 C#
5 AngularJS

参考:

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