首頁  >  文章  >  後端開發  >  C# 字典

C# 字典

WBOY
WBOY原創
2024-09-03 15:31:591038瀏覽

C#中的Dictionary類別表示為Dictionary這是一個類似於英語字典的集合,由單字集合及其按字母順序列出的多種語言的定義組成,但C# 中的字典由鍵和值集合組成,其中鍵代表單詞,值代表定義。這個字典 C#中的class屬於System.Collection.Generics命名空間,是一個泛型集合類,其中Tkey表示鍵類型,Tvalue表示Tvalue類型,IDictionary變數表示Tvalue類型。類別或字典類別可以分配給字典中的任何物件。

C#中Dictionary類別的語法如下:

IDictionary<TKey, TValue> variable_name = new Dictionary<TKey, TValue>();

Dictionary<TKey, TValue > variable_name = new Dictionary<TKey, TValue >();

C# 中 Dictionary 類別的工作

  • System.Collections.Generic命名空間中重要的類別之一是Dictionary。類。
  • 通用資料結構由 C# 中的字典類別表示,其中包含資料鍵及其對應的值。因此,任何類型的資料都可以使用字典的實例來儲存。
  • ICollection 介面的擴充是 IDictionary 介面。
  • 字典類別的Add方法用於將物件儲存在字典實例中。
  • 使用字典消除了裝箱和拆箱的開銷。

考慮下面的範例來解釋如何使用 Dictionary 類別來單獨取得鍵:

using System;
using System.Collections.Generic;
//a class called program is defined
class program
{
// main method is called
public static void Main()
{
// a new dictionary is created with key type string and value type string
Dictionary<string, string> Dict = new Dictionary<string, string>();
// using add method in dictionary to add the objects to the dictionary
Dict.Add("A", "Karnataka");
Dict.Add("B", "Maharashtra");
Dict.Add("C", "Andra");
Dict.Add("D", "TamilNadu");
Dict.Add("E", "Delhi");
Dict.Add("F", "Goa");
// Finding the number of key value pairs in the dictionary
Console.WriteLine("The number of key value pairs in the dictionary are : " + Dict.Count);
// using the property of keys to get the keys alone from the dictionary
Dictionary<string, string>.KeyCollection key =  Dict.Keys;
// a foreach loop is used to loop around every key in the dictionary and to obtain each key value
foreach(string sh in key)
{
Console.WriteLine("The key is referred as = {0}", sh);
}
}
}

上述程式的輸出如下圖:

C# 字典

上面的程式中,程式就是定義的類別。然後呼叫main方法。使用鍵類型字串和值類型字串建立一個新字典。然後使用字典中的add方法將物件新增到字典中。然後使用計數找到字典中鍵值對的數量。然後利用鍵的屬性,從字典中單獨提取鍵。然後使用 foreach 迴圈來循環字典中的每個鍵並取得每個鍵值。程式的輸出如上面的快照所示。

C# 字典的方法

C#中的Dictionary類別中有幾個方法。他們是:

1.加()

  • add() 方法用於將項目新增到字典的集合中。
  • add()方法用於將鍵值對新增至Dictionary集合中。
  • 考慮下面的範例來示範字典類別的 add 方法:
using System;
using System.Collections.Generic;
//a class called check is defined
public class Check
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str.Add(3,"Green");
str.Add(1,"Saffron");
str.Add(2,"White");
str.Add(new KeyValuePair<int, string>(4, "Blue"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str.Count);
}
}

上述程式的輸出如下圖:

C# 字典

2.  刪除()

Remove() 方法用於從字典中刪除第一次出現的指定項目。

remove() 方法用於從字典物件中刪除與 key 一起指定的元素。

考慮下面的範例來示範 Dictionary 類別中 Remove() 方法的用法:

using System;
using System.Collections.Generic;
//a class called check1 is defined
public class Check1
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str1 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str1.Add(3,"Green");
str1.Add(1,"Saffron");
str1.Add(2,"White");
str1.Add(new KeyValuePair<int, string>(4, "Blue"));
str1.Remove(1);
str1.Remove(new KeyValuePair<int, string>(2, "White"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str1.Count);
}
}

上述程式的輸出如下圖:

C# 字典

3.包含Key()

ContainsKey() 方法用於檢查給定的鍵是否存在於 Dictionary

考慮以下程式來示範 Dictionary 類別中 ContainsKey() 方法的用法:

using System;
using System.Collections.Generic;
//a class called2 check is defined
public class Check2
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str2 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str2.Add(3,"Green");
str2.Add(1,"Saffron");
str2.Add(2,"White");
str2.Add(new KeyValuePair<int, string>(4, "Blue"));
str2.Remove(1);
str2.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str2.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str2.Contains(new KeyValuePair<int, string>(2, "White")));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str2.Count);
}
}

上述程式的輸出如下圖:

C# 字典

4.包含值()

ContainsValue() 方法用於檢查給定值是否存在於 Dictionary

考慮以下程序來示範 Dictionary 類別中 ContainsValue() 方法的用法:

using System;
using System.Collections.Generic;
//a class called check3 is defined
public class Check3
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str2 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str2.Add(3,"Green");
str2.Add(1,"Saffron");
str2.Add(2,"White");
str2.Add(new KeyValuePair<int, string>(4, "Blue"));
str2.Remove(1);
str2.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str2.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str2.Contains(new KeyValuePair<int, string>(2, "White")));
//new dictionary of both string key and string value types is defined
Dictionary<string, string> stri = new Dictionary<string, string>();
stri.Add("Flag","Green");
Console.WriteLine("If the value Green is present?{0}", stri.ContainsValue("Green"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str2.Count);
}
}

上述程式的輸出如下圖:

C# 字典

5.清除()

clear()方法用於清除字典類別中的所有物件。

考慮以下程序來示範 Dictionary 類別中 ContainsValue() 方法的用法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//a class called check4 is defined
public class Check4
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str3 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str3.Add(3,"Green");
str3.Add(1,"Saffron");
str3.Add(2,"White");
str3.Add(new KeyValuePair<int, string>(4, "Blue"));
str3.Remove(1);
str3.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str3.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str3.Contains(new KeyValuePair<int, string>(2, "White")));
//new dictionary of both string key and string value types is defined
Dictionary<string, string> stri1 = new Dictionary<string, string>();
stri1.Add("Flag","Green");
Console.WriteLine("If the value Green is present?{0}", stri1.ContainsValue("Green"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str3.Count);
Console.Clear();
}
}

上述程式的輸出為空白,如下圖:

C# 字典

6. TryGetValue()

TryGetValue() 方法檢查給定的鍵是否存在,如果不存在則傳回 false。如果給定的鍵存在,則傳回 true 並將給定的值指派給指定的鍵。

Consider the below program to demonstrate the usage of TryGetValue() method of Dictionary class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//a class called check4 is defined
public class Check4
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str3 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str3.Add(3,"Green");
str3.Add(1,"Saffron");
str3.Add(2,"White");
str3.Add(new KeyValuePair<int, string>(4, "Blue"));
str3.Remove(1);
str3.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str3.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str3.Contains(new KeyValuePair<int, string>(2, "White")));
//new dictionary of both string key and string value types is defined
Dictionary<string, string> stri1 = new Dictionary<string, string>();
stri1.Add("Flag","Green");
Console.WriteLine("If the value Green is present?{0}", stri1.ContainsValue("Green"));
string res;
if(str3.TryGetValue(4, out res))
{
Console.WriteLine("The value of the specified key is {0}", res);
}
else
{
Console.WriteLine("The specified key is not present.");
}
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str3.Count);
}
}

The output of the above program is as shown in the snapshot below:

C# 字典

Conclusion

In this tutorial, we understand the concept of Dictionary class in C# through definition, the syntax of Dictionary class in C#, working of Dictionary class, and their methods through programming examples and their outputs.

以上是C# 字典的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 非同步下一篇:C# 非同步