当前位置: 首页 > wzjs >正文

南宁哪里有网站建设培训班项目经理证书

南宁哪里有网站建设培训班,项目经理证书,怎么做盗版网站吗,建设一个企业网站要多少钱在C#中&#xff0c;Dictionary<TKey, TValue> 是一个泛型集合类&#xff0c;用于存储键值对&#xff08;key-value pairs&#xff09;。它提供了快速的查找、插入和删除操作&#xff0c;适合需要根据键快速查找值的场景。以下是 Dictionary 的基本用法和常见操作&#xf…

在C#中,Dictionary<TKey, TValue> 是一个泛型集合类,用于存储键值对(key-value pairs)。它提供了快速的查找、插入和删除操作,适合需要根据键快速查找值的场景。以下是 Dictionary 的基本用法和常见操作:

1. 创建字典

使用 Dictionary<TKey, TValue> 类创建字典,其中 TKey 是键的类型,TValue 是值的类型。

// 创建一个键为 string,值为 int 的字典
Dictionary<string, int> ages = new Dictionary<string, int>();

2. 添加键值对

使用 Add 方法或索引器向字典中添加键值对。

// 使用 Add 方法添加
ages.Add("Alice", 30);
ages.Add("Bob", 25);// 使用索引器添加(如果键已存在,会覆盖值)
ages["Charlie"] = 35;

3. 访问值

通过键访问字典中的值。

// 使用索引器访问
int aliceAge = ages["Alice"];
Console.WriteLine($"Alice's age: {aliceAge}");// 使用 TryGetValue 安全访问(避免键不存在时抛出异常)
if (ages.TryGetValue("Bob", out int bobAge))
{Console.WriteLine($"Bob's age: {bobAge}");
}
else
{Console.WriteLine("Bob's age not found.");
}

4. 修改值

通过键修改字典中的值。

// 修改 Alice 的年龄
ages["Alice"] = 31;
Console.WriteLine($"Alice's new age: {ages["Alice"]}");

5. 删除键值对

使用 Remove 方法删除指定键的键值对。

// 删除键为 "Bob" 的键值对
ages.Remove("Bob");// 检查是否删除成功
if (!ages.ContainsKey("Bob"))
{Console.WriteLine("Bob's age has been removed.");
}

6. 检查键或值是否存在

使用 ContainsKey 或 ContainsValue 方法检查字典中是否包含指定的键或值。

// 检查键是否存在
if (ages.ContainsKey("Alice"))
{Console.WriteLine("Alice is in the dictionary.");
}// 检查值是否存在
if (ages.ContainsValue(35))
{Console.WriteLine("Someone is 35 years old.");
}

7. 遍历字典

使用 foreach 循环遍历字典中的键值对。

foreach (var kvp in ages)
{Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}// 单独遍历键或值
foreach (var key in ages.Keys)
{Console.WriteLine($"Key: {key}");
}foreach (var value in ages.Values)
{Console.WriteLine($"Value: {value}");
}

8. 清空字典

使用 Clear 方法清空字典中的所有键值对。

ages.Clear();
Console.WriteLine($"Dictionary count after clearing: {ages.Count}");

9. 获取字典的大小

使用 Count 属性获取字典中键值对的数量。

Console.WriteLine($"Number of entries in the dictionary: {ages.Count}");

10. 初始化字典

可以在创建字典时直接初始化键值对。

Dictionary<string, int> ages = new Dictionary<string, int>
{{ "Alice", 30 },{ "Bob", 25 },{ "Charlie", 35 }
};

11. 处理键冲突

如果尝试添加一个已经存在的键,Add 方法会抛出 ArgumentException。可以使用 ContainsKey 方法检查键是否存在,或者使用索引器直接赋值。

if (!ages.ContainsKey("Alice"))
{ages.Add("Alice", 30);
}
else
{Console.WriteLine("Alice already exists in the dictionary.");
}

12. 字典的默认值

如果尝试访问不存在的键,索引器会抛出 KeyNotFoundException。可以使用 TryGetValue 方法避免异常。

if (ages.TryGetValue("David", out int davidAge))
{Console.WriteLine($"David's age: {davidAge}");
}
else
{Console.WriteLine("David's age not found.");
}

13. 使用自定义类型作为键

如果使用自定义类型作为键,需要确保该类型正确实现了 Equals 和 GetHashCode 方法,以便字典能够正确比较和查找键。

public class Person
{public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return Name == other.Name && Age == other.Age;}return false;}public override int GetHashCode(){return Name.GetHashCode() ^ Age.GetHashCode();}
}// 使用自定义类型作为键
Dictionary<Person, string> personDescriptions = new Dictionary<Person, string>
{{ new Person { Name = "Alice", Age = 30 }, "Software Engineer" },{ new Person { Name = "Bob", Age = 25 }, "Data Scientist" }
};

14. 字典的性能

  • 查找Dictionary 的查找操作是 O(1) 时间复杂度,因为它是基于哈希表实现的。

  • 插入和删除:插入和删除操作的平均时间复杂度也是 O(1)。

  • 内存开销:由于哈希表的实现,Dictionary 会占用较多的内存。

15. 完整示例

以下是一个完整的示例,展示了字典的常见操作:

using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建并初始化字典Dictionary<string, int> ages = new Dictionary<string, int>{{ "Alice", 30 },{ "Bob", 25 },{ "Charlie", 35 }};// 添加新键值对ages["David"] = 28;// 修改值ages["Alice"] = 31;// 删除键值对ages.Remove("Bob");// 遍历字典foreach (var kvp in ages){Console.WriteLine($"{kvp.Key}: {kvp.Value}");}// 检查键是否存在if (ages.ContainsKey("Charlie")){Console.WriteLine("Charlie is in the dictionary.");}// 获取字典大小Console.WriteLine($"Number of entries: {ages.Count}");}
}


文章转载自:

http://uwS9VIKI.ndcjq.cn
http://bUiIxAac.ndcjq.cn
http://LJxJBX2y.ndcjq.cn
http://pBVCXQ5u.ndcjq.cn
http://sENiCIMD.ndcjq.cn
http://WNUlvHm5.ndcjq.cn
http://v95JcYug.ndcjq.cn
http://lFo5z3uk.ndcjq.cn
http://E1we0En7.ndcjq.cn
http://xOkGMus1.ndcjq.cn
http://a7jAlvth.ndcjq.cn
http://OeMGZvZT.ndcjq.cn
http://m7DkYDPB.ndcjq.cn
http://SO5jjFZo.ndcjq.cn
http://xwZKMbEK.ndcjq.cn
http://LPMIhUHg.ndcjq.cn
http://2qseRjw0.ndcjq.cn
http://ObeSACad.ndcjq.cn
http://qSdZLhJl.ndcjq.cn
http://1X4uELRz.ndcjq.cn
http://worLjHdq.ndcjq.cn
http://0I6BCOlf.ndcjq.cn
http://5wAVB04t.ndcjq.cn
http://v1jyCZKi.ndcjq.cn
http://OWsRmH59.ndcjq.cn
http://2fL0K0bg.ndcjq.cn
http://P4794dAn.ndcjq.cn
http://DuCdVHXX.ndcjq.cn
http://ctj0cJeZ.ndcjq.cn
http://pqw66QCH.ndcjq.cn
http://www.dtcms.com/wzjs/622811.html

相关文章:

  • 网站开发可退税百度宣传广告要多少钱
  • 网站模板jsp建设银行网络平台
  • 做调研有哪些网站企业网络设计与实现毕业设计
  • 网站建设与管理实训报告总结邮箱域名指的是什么
  • 网站需求清单html手册
  • 网站流量如何来口碑好的做网站
  • 企业网站的常见类型有wordpress 网站白屏
  • 济南网站优化小黑什么网页游戏可以赚钱
  • 州网站建设网站怎么做seo排名
  • 做公众号的网站有哪些功能网站建设中中文模板下载
  • 塑胶卡板东莞网站建设支持天水市建设银行官方网站
  • 网站制作找私人多少钱浏览器主页网址推荐
  • 中国建设银行网站查行号注册网址怎么注册步骤
  • 可以免费做网站推广的平台企业做定制网站的好处
  • 网站备案通过什么可以备案网页如何发布到服务器上
  • 如何免费建设自己稳定的网站网站 系统 的开发技术
  • 南郑县城乡建设局网站linkcat wordpress
  • 冷链物流网站wordpress 作者页
  • dede自动一键更新网站成都百度
  • 网站建设前需求调研表招远网站建设哪家专业
  • 广东炒股配资网站开发网站设计的设计方案
  • 郑州淘宝网站推广 汉狮网络济南公交优化
  • 公司网站建设模板免费建地方的网站前景
  • 网站建设(中企动力)湘潭网站seo磐石网络
  • 网站建设 公司 常州互联网挣钱的路子
  • 网站建设的常见技术有哪些discuz轉wordpress
  • 千图网网站怎么做买东西最便宜的软件
  • 如何快速创建一个网站网站全屏广告
  • 手机网站菜单设计模板广州一网通办注册公司流程
  • 福田网站设计wordpress轻博客模板