[Dotnet][C#]Dictionary的foreach使用KeyValuePair

本文通过一个ASP.NET示例介绍了如何在C#中使用KeyValuePair来遍历字典,并展示了具体的实现代码及运行结果。

foreach (KeyValuePair<int, string> kvp in myDictionary) {...}

看到這句就大概可以猜出來了,就是用KeyValuePair,用范例介紹如何使用..

asp.net(c#)
TestKeyValuePair.aspx.cs

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Collections.Generic;  
  12.   
  13. public partial class TestKeyValuePair : System.Web.UI.Page  
  14. {  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.      {  
  17.          Dictionary<string, string> dic = new Dictionary<string, string>();  
  18.   
  19.          dic.Add("Name", "puma");  
  20.          dic.Add("Blog", "F6 Team");  
  21.          dic.Add("Group", "Dotblogs");  
  22.   
  23.         foreach (KeyValuePair<string, string> item in dic)  
  24.          {  
  25.              Response.Write(string.Format("{0} : {1}<br/" + ">", item.Key, item.Value));  
  26.          }  
  27.   
  28.      }  
  29. }  

 

 

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Generic; public partial class TestKeyValuePair : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("Name", "puma"); dic.Add("Blog", "F6 Team"); dic.Add("Group", "Dotblogs"); foreach (KeyValuePair<string, string> item in dic) { Response.Write(string.Format("{0} : {1}<br/" + ">", item.Key, item.Value)); } } }

执行結果:

 

<think>好的,用户想知道在Unity中如何使用List和KeyValuePair。我需要先回忆一下这两个结构的基本概念和常见用法。首先,List<T>是C#中的泛型集合,用于动态存储元素,而KeyValuePair<TKey, TValue>通常用于字典中的键值对存储。 根据用户提供的引用内容,引用[2]和引用[3]提到了DictionaryKeyValuePair的用法,比如如何添加键值对和遍历它们。引用[1]中的SortedList也涉及到键值对的存储,不过用户的问题更关注List和KeyValuePair本身。 首先,我应该解释List的基本用法,包括初始化、添加元素、访问和遍历。例如,在Unity中创建一个List<int>或者List<string>,然后演示如何通过Add方法添加元素,使用foreach循环遍历。需要确保代码示例符合Unity的语法,比如在Start方法中使用。 接下来是KeyValuePair,它通常与Dictionary一起使用,所以需要提到Dictionary的用法。比如如何声明一个Dictionary<int, object>,然后添加键值对,再通过foreach循环遍历每个KeyValuePair。引用[2]中的代码可以作为例子,用户展示如何添加不同类型的值,比如Vector2、Vector3和字符串,并输出键值对。 另外,可能需要提到单独使用KeyValuePair的情况,虽然不常见,但可以举例说明如何创建一个KeyValuePair变量,并访问其Key和Value属性。例如,var pair = new KeyValuePair<int, string>(1, "Unity");,然后使用pair.Key和pair.Value获取数据。 还要注意用户可能在Unity中使用这些集合进行游戏开发,比如管理游戏对象、存储角色属性等。需要给出实际应用场景的例子,比如用List存储玩家物品,用Dictionary管理角色状态,这样用户更容易理解如何应用这些结构。 需要确保回答中的代码示例正确,并且符合用户提供的引用中的格式,比如使用正确的泛型语法和Add方法。同时,要提醒用户注意Dictionary的键唯一性,以及List的动态扩容特性,避免常见错误,比如重复键或索引越界。 最后,生成相关问题,可能涉及性能优化、排序集合的使用,或者与其他数据结构的比较,比如引用[1]中的SortedList和SortedDictionary,引导用户进一步学习。</think>在Unity中,`List`和`KeyValuePair`是两种常用的数据结构,分别用于动态集合存储和键值对管理。以下是具体用法说明: --- ### 一、List的用法 `List<T>`是泛型动态数组,可存储任意类型的元素,支持动态扩容。 #### 1. 基础操作 ```csharp using System.Collections.Generic; using UnityEngine; public class ListExample : MonoBehaviour { void Start() { // 创建List并添加元素 List<int> numbers = new List<int>(); numbers.Add(10); numbers.Add(20); // 访问元素 Debug.Log(numbers[0]); // 输出: 10 // 遍历 foreach (int num in numbers) { Debug.Log(num); } } } ``` #### 2. 实际应用场景 - 存储玩家背包物品:`List<Item> inventory = new List<Item>();` - 管理游戏对象池:`List<GameObject> enemyPool = new List<GameObject>();` --- ### 二、KeyValuePair的用法 `KeyValuePair<TKey, TValue>`通常与`Dictionary<TKey, TValue>`配合使用,用于存储键值对[^2]。 #### 1. 字典的声明与遍历 ```csharp using System.Collections.Generic; using UnityEngine; public class DictionaryExample : MonoBehaviour { void Start() { Dictionary<int, string> playerStatus = new Dictionary<int, string>(); playerStatus.Add(1, "生命值: 100"); playerStatus.Add(2, "攻击力: 50"); // 遍历键值对 foreach (KeyValuePair<int, string> pair in playerStatus) { Debug.Log($"ID: {pair.Key}, 状态: {pair.Value}"); } } } ``` #### 2. 独立使用KeyValuePair ```csharp // 创建单个键值对 KeyValuePair<string, Vector3> positionData = new KeyValuePair<string, Vector3>("Player", new Vector3(0, 0, 0)); Debug.Log($"对象名: {positionData.Key}, 坐标: {positionData.Value}"); ``` --- ### 三、结合使用场景示例 ```csharp // 用List存储多个字典 List<Dictionary<string, int>> playerStatsList = new List<Dictionary<string, int>>(); Dictionary<string, int> stats1 = new Dictionary<string, int>(); stats1.Add("HP", 100); playerStatsList.Add(stats1); ``` --- ### 注意事项 1. **字典键唯一性**:若添加重复键会抛出`ArgumentException`,需先用`ContainsKey`检查[^2]。 2. **List性能**:频繁插入/删除时考虑使用`LinkedList`优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值