C#中字典Dictionary的用法详解

1.字典的类型

public class userscore
{
    public List<string> fen; //分数
    public List<string> time; //时间
}
  public Dictionary<string, userscore> DicScore = new Dictionary<string, userscore>(); //账户 分数

2.根据字典的值取字典的键

/// 
    /// 根据字典的值取字典的键
    /// 
    /// 字典的键
    /// 字典键的值
    public string Dictionary_Key(string str)
    {
        string strr = "";
        //BaoCunScoreTime.instance.DicScore 是字典
        foreach (var item in BaoCunScoreTime.instance.DicScore)
        {
            foreach (var itrr in item.Value.fen)
            {
                //判断字典里有没有这个分数(字典的值),有了返回出来
                if (itrr.Equals (str))
                {
                    strr= item.Key;                   
                }
            }
        }
        return strr;
    }

3.根据字典的键取字典的值

//返回账户分数的最大值
    /// 
    /// 根据字典的键取字典的值
    /// 
    /// 字典的键
    /// 字典里最大分值
    public string  FanhuiZuiDaZhi(string str)
    {
        List<int> liststr = new List<int>();
        //因为分数有很多,需要找到最大的一个
        foreach (var item in BaoCunScoreTime.instance.DicScore[str].fen)
        {          
            liststr.Add(int.Parse(item));
        }
        int sun=0;
        foreach (var item in liststr)
        {
            if (item >sun)
            {
                sun = item;
            }
        }       
        return sun.ToString ();
    }

4. 根据字典键确定 值中最高分的时间

//根据字典键确定 值中最高分的时间
    public string ScoreZuiDaZhiTime(string str)
    {
        List<int> liststr = new List<int>();
        List<string> liststring = new List<string>();
        foreach (var item in BaoCunScoreTime.instance.DicScore[str].fen)
        {
            liststr.Add(int.Parse(item));
        }
        foreach (var item in BaoCunScoreTime.instance.DicScore[str].time)
        {
            liststring.Add(item);
        }
        int sun = 0;
        foreach (var item in liststr)
        {
            if (item > sun)
            {
                sun = item;
            }
        }
        string suning="";
        for (int i = 0; i < liststr.Count; i++)
        {
            if (liststr [i]==sun)
            {
                suning = liststring[i];
            }
        }
        return suning;
    }
 /// 
    /// 根据键返回字典的值
    /// 
    /// 字典
    /// 
    /// 
    public GameObject GetDicValue(Dictionary<Transform, GameObject> dicdata, Transform Key_)
    {
        GameObject trans = null;
        if (dicdata.ContainsKey(Key_))
        {
            trans = dicdata[Key_];
        }
        return trans;
    }
    /// 
    /// 根据值返回字典的键
    /// 
    /// 字典
    /// 
    /// 
    public Transform GetDicKey(Dictionary<Transform, GameObject> dicdata,GameObject value_)
    {
        Transform trans = null;
        if (dicdata.ContainsValue(value_))
        {
            foreach (var item in dicdata)
            {
                if (item.Value.Equals(value_))
                {
                    trans = item.Key;
                }
            }
        }
        return trans;

    }

简单记录一下

C#中字典Dictionary的用法详解_第1张图片

你可能感兴趣的:(c#,开发语言,unity)