Home >> Blog >> c# dictionary 範例分享

放下手中的SEO案子,現在我們聊聊c# dictionary

c# dictionary 範例分享

在 C# 中,Dictionary 是一個通用集合,通常用於存儲鍵/值對。Dictionary 的工作方式與非泛型 hashtable非常相似。Dictionary 的優點是,它是泛型類型。dictionary在命名空間下定義System.Collection.Generic。它本質上是動態的,意味著dictionary的大小根據需要而增長。

如何創建dictionary?

Dictionary 類有7 個構造函數用於創建 Dictionary,這裡我們只使用 Dictionary() 構造函數,如果你想了解更多關於構造函數的信息,請參考C# | dictionary類。

Dictionary():此構造函數用於創建 Dictionary 類的實例,該實例為空,具有默認初始容量,並為鍵類型使用默認相等比較器,如下所示:

第 1 步:在 using 關鍵字的幫助下,在程式中包含 System.Collection.Generic 命名空間。

句法:

使用 System.Collection.Generic;

第 2 步:使用 Dictionary 類創建dictionary,如下所示:

dictionary dictionary_name = new Dictionary();

第 3 步:如果要在 Dictionary 中添加元素,請使用Add()方法在 Dictionary 中添加鍵/值對。您還可以在dictionary中添加鍵/值對,而無需使用 Add 方法。如下例所示。

第 4 步:使用三種不同的方式訪問 Dictionary 的鍵/值對:

. for 循環:您可以使用 for 循環來訪問 Dictionary 的鍵/值對。 例子:

for(int x=0; i< My_dict1.Count; x++)
{
Console.WriteLine("{0} and {1}", My_dict1.Keys.ElementAt(x),
My_dict1[ My_dict1.Keys.ElementAt(x)]);
}

使用索引:您可以使用dictionary的索引值訪問dictionary的單個鍵/值對。在這裡,您只需指定索引中的鍵即可從給定dictionary中獲取值,無需指定索引。索引器總是將鍵作為參數,如果給定的鍵在dictionary中不可用,那麼它會給出KeyNotFoundException。 例子:

Console.WriteLine("Value is:{0}", My_dicti[1123]);
Console.WriteLine("Value is:{0}", My_dicti[1125]);

foreach 循環:您可以使用 foreach 循環訪問dictionary的鍵/值對。如下例所示,我們使用 foreach 循環訪問dictionary。

例子:

// C# program to illustrate how
// to create a dictionary
using System;
using System.Collections.Generic;

class GFG {

// Main Method
static public void Main () {

// Creating a dictionary
// using Dictionary< TKey,TValue> class
Dictionary< int, string> My_dict1 =
new Dictionary();

// Adding key/value pairs
// in the Dictionary
// Using Add() method
My_dict1.Add(1123, "Welcome");
My_dict1.Add(1124, "to");
My_dict1.Add(1125, "GeeksforGeeks");

foreach(KeyValuePair< int, string> ele1 in My_dict1)
{
Console.WriteLine("{0} and {1}",
ele1.Key, ele1.Value);
}
Console.WriteLine();

// Creating another dictionary
// using Dictionary< TKey,TValue> class
// adding key/value pairs without
// using Add method
Dictionary< string, string> My_dict2 =
new Dictionary< string, string>(){
{"a.1", "Dog"},
{"a.2", "Cat"},
{"a.3", "Pig"} };

foreach(KeyValuePair< string, string> ele2 in My_dict2)
{
Console.WriteLine("{0} and {1}", ele2.Key, ele2.Value);
}
}
}

輸出:

1123和歡迎
1124 至
1125 和 GeeksforGeeks

a.1 和狗
a.2 和貓
a.3 和豬

如何從dictionary中刪除元素?

在 Dictionary 中,您可以從 Dictionary 中刪除元素。Dictionary 類提供了兩種不同的方法來刪除元素,方法是:

.Clear:此方法從 Dictionary 中刪除所有鍵和值。
.Remove:此方法從 Dictionary 中刪除具有指定鍵的值。

例子:

// C# program to illustrate how
// remove key/value pairs from
// the dictionary
using System;
using System.Collections.Generic;

class GFG {

// Main Method
static public void Main() {

// Creating a dictionary
// using Dictionary< TKey,TValue> class
Dictionary< int, string> My_dict =
new Dictionary< int, string>();

// Adding key/value pairs in the
// Dictionary Using Add() method
My_dict.Add(1123, "Welcome");
My_dict.Add(1124, "to");
My_dict.Add(1125, "GeeksforGeeks");

// Before Remove() method
foreach(KeyValuePair< int, string> ele in My_dict)
{
Console.WriteLine("{0} and {1}",
ele.Key, ele.Value);
}
Console.WriteLine();

// Using Remove() method
My_dict.Remove(1123);

// After Remove() method
foreach(KeyValuePair< int, string> ele in My_dict)
{
Console.WriteLine("{0} and {1}",
ele.Key, ele.Value);
}
Console.WriteLine();


// Using Clear() method
My_dict.Clear();

Console.WriteLine("Total number of key/value "+
"pairs present in My_dict:{0}", My_dict.Count);

}
}

輸出:

1123和歡迎
1124 至
1125 和 GeeksforGeeks

1124 至
1125 和 GeeksforGeeks

My_dict 中存在的鍵/值對總數:0

如何檢查dictionary中元素的可用性?
在 Dictionary 中,您可以檢查給定的鍵或值是否存在於指定的dictionary中。Dictionary 類提供了兩種不同的檢查方法,方法是:

.ContainsKey:該方法用於檢查 Dictionary 是否包含指定的鍵。
.ContainsValue:此方法用於檢查 Dictionary 是否包含特定值。

例子:

// C# program to illustrate how
// to check the given key or
// value present in the dictionary
// or not
using System;
using System.Collections.Generic;

class GFG {

// Main Method
static public void Main () {

// Creating a dictionary
// using Dictionary< TKey,TValue> class
Dictionary< int, string> My_dict =
new Dictionary< int, string>();

// Adding key/value pairs in the
// Dictionary Using Add() method
My_dict.Add(1123, "Welcome");
My_dict.Add(1124, "to");
My_dict.Add(1125, "GeeksforGeeks");

// Using ContainsKey() method to check
// the specified key is present or not
if (My_dict.ContainsKey(1122)==true)
{
Console.WriteLine("Key is found...!!");
}

else
{
Console.WriteLine("Key is not found...!!");
}

// Using ContainsValue() method to check
// the specified value is present or not
if (My_dict.ContainsValue("GeeksforGeeks")==true)
{
Console.WriteLine("Value is found...!!");
}

else
{
Console.WriteLine("Value is not found...!!");
}
}
}

輸出:

沒有找到鑰匙……!!
找到了價值……!!