温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Dictionary怎么在C#中使用

发布时间:2021-04-12 15:50:43 来源:亿速云 阅读:274 作者:Leah 栏目:开发技术

这篇文章给大家介绍Dictionary怎么在C#中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Dictionary需要注意的特性

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

Dictionary怎么在C#中使用

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

Dictionary怎么在C#中使用

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

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

Dictionary怎么在C#中使用

Dictionary怎么在C#中使用

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

Dictionary怎么在C#中使用

Dictionary怎么在C#中使用

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

Dictionary怎么在C#中使用

Dictionary怎么在C#中使用

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

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

if(myDictionary.ContainsKey(key))
{
    // 通过key索引value
    int resValue = myDictionary[key];
}

法二:TryGetValue的方法:

int resValue ;
myDictionary.TryGetValue(key, out resValue);

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

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

用法

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

// 声明和初始化
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);

实例应用

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)

字典的使用方法:

定义:

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.");
    }

参数为其他类型

//参数为其它类型 
    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]);

参数为自定义类型

  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。)




关于Dictionary怎么在C#中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI