服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#字典Dictionary的用法说明(注重性能版)

C#字典Dictionary的用法说明(注重性能版)

2022-11-10 14:33Real_JumpChen C#

这篇文章主要介绍了C#字典Dictionary的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

前言

以键值对Dictionary<[key], [value]>形式存值,和哈希表很像也是一种无序的结构。

要使用Dictionary,需要先导入C#泛型命名空间System.Collections.Generic

Dictionary需要注意的特性

1.任何键都必须是唯一的 ——> 不能添加相同key的键值对,不然就报错:

C#字典Dictionary的用法说明(注重性能版)

如果要修改已有key对应的value,可以这样做:

C#字典Dictionary的用法说明(注重性能版)

2.Unity5.4以下的版本,最好不要用foreach来遍历字典:

法一:foreach遍历字典,会生成GC:

C#字典Dictionary的用法说明(注重性能版)

C#字典Dictionary的用法说明(注重性能版)

法二:对于我这种需求,使用for循环,会生成更多的GC,因为存在mActMergeRedPointKey这个局部List变量

C#字典Dictionary的用法说明(注重性能版)

C#字典Dictionary的用法说明(注重性能版)

法三:使用迭代器,不会生成GC:

C#字典Dictionary的用法说明(注重性能版)

C#字典Dictionary的用法说明(注重性能版)

3.根据key取value,最好使用 TryGetValue 而不是 ContainsKey+根据key索引value:

法一:ContainsKey+根据key索引value,不好,用了两次查找,第一次:ContainsKey,第二次:myDictionary[key]

?
1
2
3
4
5
if(myDictionary.ContainsKey(key))
{
    // 通过key索引value
    int resValue = myDictionary[key];
}

法二:TryGetValue的方法:

?
1
2
int resValue ;
myDictionary.TryGetValue(key, out resValue);

使用TryGetValue更快,性能更好,因为只用了一次查找,TryGetValue 比 ContainsKey后使用[key]取value,速度快一倍;

TryGetValue更安全,找不到value时返回false;而使用ContainsKey后使用[key]取value取不到时,会抛出异常导致真机卡死。

用法

一般用法:key和value都为基本类型,举例:key为int类型,value为string类型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 声明和初始化
Dictionary<int,string> myDictionary = new Dictionary<int,string>();
// 添加元素
myDictionary.Add(key,value);
// 判断是否包含键
if(myDictionary.ContainsKey(key))
// 总个数
myDictionary.Count
// 遍历
foreach(string key in myDictionary.Keys) // myDictionary.Keys:所有键的集合
{
    int resValue = myDictionary[key];
}
//调用成员Keys,会产生额外GC:Dictionary本身储存数据是成对储存的,也就是KeyValuePair,所以
//要单独拿出Keys时会新建一个数组,这也使得GC增加,推荐大家如果不需要单独存储Keys,尽量避免调用。
// 移除指定键和值
myDictionary.Remove(key);

实例应用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private Dictionary<uint, MyPet> myPets;
public List<MyPet> GetShowPets()
{
    List<MyPet> pets = new List<MyPet>();
    if (null != myPets)
    {
        var e = myPets.GetEnumerator();
        while (e.MoveNext())
        {
            if (CheckPetShow(e.Current.Key))
            {
                pets.Add(e.Current.Value);
            }
        }
    }
    //根据配置表权重进行升序排序
    pets.Sort(
        delegate (MyPet pet1, MyPet pet2)
        {
            return pet1.PetRankWeight.CompareTo(pet2.PetRankWeight);
        });
    return pets;
}

补充:c#中字典类(Dictionary)介绍

关键字:Dictionary

说明:

1、必须包含命名空间System.Collection.Generic

2、Dictionary里面每一个元素都是以键值对的形式存在的

3、键必须是唯一的,而值不需要唯一的

4、键和值都可以以任何数据类型存在(比如:值类型、引用类型、自定义类型等等)

5、通过一个键读取一个值得时间接近O(1)

字典的使用方法:

定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Dictionary<string,string> openWith = new Dictionary<string,string>();
//添加元素
 openWith.Add("txt", "notepad.exe");
 openWith.Add("bmp", "paint.exe");
 openWith.Add("dib", "paint.exe");
 openWith.Add("rtf", "wordpad.exe");
 openWith["png"] = "picture.exe"
//取值
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
//更改值
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
 //遍历key
foreach (string key in openWith.Keys)
{
    Console.WriteLine("Key = {0}", key);
}
//遍历value
    foreach (string value in openWith.Values)
    {
        Console.WriteLine("value = {0}", value);
    }
 
    //遍历value, Second Method
    Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
    foreach (string s in valueColl)
    {
        Console.WriteLine("Second Method, Value = {0}", s);
    }
 //遍历字典
    foreach (KeyValuePair<string, string> kvp in openWith)
    {
        Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
    }
//添加存在的元素
    try
    {
        openWith.Add("txt", "winword.exe");
    }
    catch (ArgumentException)
    {
        Console.WriteLine("An element with Key = \"txt\" already exists.");
    }
//删除元素
    openWith.Remove("doc");
    if (!openWith.ContainsKey("doc"))
    {
        Console.WriteLine("Key \"doc\" is not found.");
    }
//判断键存在
    if (openWith.ContainsKey("bmp")) // True
    {
        Console.WriteLine("An element with Key = \"bmp\" exists.");
    }

参数为其他类型

?
1
2
3
4
5
//参数为其它类型
    Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
    OtherType.Add(1, "1,11,111".Split(','));
    OtherType.Add(2, "2,22,222".Split(','));
    Console.WriteLine(OtherType[1][2]);

参数为自定义类型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class DouCube
  {
      public int Code{get { return _Code; } set { _Code = value; } }
      private int _Code;
      public string Page { get { return _Page; } set { _Page = value; } }
      private string _Page;
  }
  //声明并添加元素
  Dictionary<int, DouCube> MyType = new Dictionary<int, DouCube>();
  for (int i = 1; i <= 9; i++)
  {
      DouCube element = new DouCube();
      element.Code = i * 100;
      element.Page = "http://www.doucube.com/" + i.ToString() + ".html";
      MyType.Add(i, element);
  }
  //遍历元素
  foreach (KeyValuePair<int, DouCube> kvp in MyType)
  {
      Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
  }

常用属性

名称 说明
Comparer 获取用于确定字典中的键是否相等的 IEqualityComparer<T>。
Count 获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目
Item 获取或设置与指定的键相关联的值。
Keys 获取包含 Dictionary<TKey, TValue> 中的键的集合。
Values 获取包含 Dictionary<TKey, TValue> 中的值的集合。

常用方法

名称 说明
Add 将指定的键和值添加到字典中。
Clear 从 Dictionary<TKey, TValue> 中移除所有的键和值
ContainsKey 确定 Dictionary<TKey, TValue> 是否包含指定的键
ContainsValue 确定 Dictionary<TKey, TValue> 是否包含特定值
GetEnumerator 返回循环访问 Dictionary<TKey, TValue> 的枚举器
GetObjectData 实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> 实例所需的数据
GetType 获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
OnDeserialization 实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
Remove 从 Dictionary<TKey, TValue> 中移除所指定的键的值 TryGetValue 获取与指定的键相关联的值。
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。(继承自 Object。)
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
   
   

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/BillCYJ/article/details/81015598

延伸 · 阅读

精彩推荐
  • C#C#向线程中传递多个参数的解决方法(两种)

    C#向线程中传递多个参数的解决方法(两种)

    这篇文章主要介绍了C#向线程中传递多个参数的解决方法(两种)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    C#教程网10292021-12-02
  • C#在C# WPF下自定义滚动条ScrollViewer样式的操作

    在C# WPF下自定义滚动条ScrollViewer样式的操作

    这篇文章主要介绍了在C# WPF下自定义滚动条ScrollViewer样式的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    凡梦_5452022-10-27
  • C#C#装箱和拆箱操作实例分析

    C#装箱和拆箱操作实例分析

    这篇文章主要介绍了C#装箱和拆箱操作,结合实例形式分析了C#中装箱与拆箱的概念、用法及相关注意事项,需要的朋友可以参考下...

    Quber6682021-12-02
  • C#Unity制作图片字体的方法

    Unity制作图片字体的方法

    这篇文章主要为大家详细介绍了Unity制作图片字体的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    422251853292022-10-25
  • C#C#3.0使用EventLog类写Windows事件日志的方法

    C#3.0使用EventLog类写Windows事件日志的方法

    这篇文章主要介绍了C#3.0使用EventLog类写Windows事件日志的方法,以简单实例形式分析了C#写windows事件日志的技巧,具有一定参考借鉴价值,需要的朋友可以参考...

    我心依旧5792021-10-22
  • C#WPF中引入WindowsForms控件的方法

    WPF中引入WindowsForms控件的方法

    这篇文章主要介绍了WPF中引入WindowsForms控件的方法,结合实例形式分析了在WPF中调用Windows Forms控件的具体步骤与相关实现技巧,需要的朋友可以参考下...

    kagula9812021-11-30
  • C#c# 并行和多线程编程——认识Parallel

    c# 并行和多线程编程——认识Parallel

    这篇文章主要介绍了c# 并行和多线程编程的相关资料,帮助大家更好的理解和学习c# Parallel的相关知识,感兴趣的朋友可以了解下...

    雲霏霏11442022-11-02
  • C#C#实现飞行棋(Winform)

    C#实现飞行棋(Winform)

    这篇文章主要为大家详细介绍了基于Winform框架的飞行棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    搁°浅7122022-08-24