2016年6月13日 星期一

C#的Dictionary之取得資料

Dictionary是類似Hashtable的架構,用Key和Value的方式來相對應。
而要來依序取得裡面的資料時,foreach裡引用的架構和一般有點不同,主要是keyValuePair。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class MainProcess : MonoBehaviour {
  6.  
  7.     private Dictionary<int, string> tempDictionary = new Dictionary<int, string>();
  8.  
  9.     void Start ()
  10.     {
  11.         tempDictionary.Add( 1, "XXX" );
  12.         tempDictionary.Add( 2, "YYY" );
  13.         tempDictionary.Add( 3, "ZZZ" );
  14.  
  15.         foreach( KeyValuePair<int, string> x in tempDictionary )
  16.         {
  17.             Debug.Log( x.Key + " = " + x.Value );
  18.         }
  19.     }
  20. }

沒有留言:

張貼留言