首页  >  文章  >  后端开发  >  C# 字典

C# 字典

WBOY
WBOY原创
2024-09-03 15:31:591050浏览

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# Asynchronous下一篇:IEnumerable C#