C# Unity Dictionary 根據下標獲取當前鍵值對、 值


我們常用的 知道 C#   的  Dictionary 獲取 值可以有兩種方式 :例如:

Dictionary<int, string> dic = new Dictionary<int, string>();

dic.Add(5, "5555");
dic.Add(6, "6666");
dic.Add(7, "7777");
dic.Add(8, "8888");
dic.Add(9, "9999");

獲取 值可以有兩種方式 (Unity3D 中運行):

1. 方式一:【Try Get Value】

例如:

string value = null;
dic.TryGetValue(5, out value); 

Debug.Log(value);          ▶打印結果是 5555

2.方式二:【字典名[ 鍵的名字]  】

例如

Debug.Log(dic[5]);          ▶打印結果是 5555

---------------------------------------------------------------------

3.但是 如果我們想像數組那樣,根據下標,獲取值,那有啥方法呢?  

可以使用 :   【字典名.ElementAt ( 角標 ).Value】

導入:using System.Linq;  命名空間

例如:

dic.ElementAt(4).Value;       ▶打印結果是: 99999 

dic.ElementAt(4);           ▶打印結果是: [9, 99999]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM