unity3d Dictionary 根据key获取value

unity3d Dictionary 根据key获取value
 
 
using System;
using System.Collections.Generic;
using UnityEngine;

public class DictionaryExample : MonoBehaviour
{
    private Dictionary<string, int> myDictionary;

    void Start()
    {
        // 初始化字典并添加一些键值对
        myDictionary = new Dictionary<string, int>();
        myDictionary.Add("One", 1);
        myDictionary.Add("Two", 2);
        myDictionary.Add("Three", 3);

        // 根据键获取值
        string keyToSearch = "Two";
        if (myDictionary.ContainsKey(keyToSearch))
        {
            int value = myDictionary[keyToSearch];
            Debug.Log("The value for key '" + keyToSearch + "' is: " + value);
        }
        else
        {
            Debug.Log("The key '" + keyToSearch + "' does not exist in the dictionary.");
        }
    }
}

 

 

 

 

###########################

posted @ 2024-07-21 22:26  西北逍遥  阅读(59)  评论(0编辑  收藏  举报