【C#日常】C#集合Dictionary获取第一个键值

75 篇文章 1 订阅
9 篇文章 0 订阅

一、前言
今天分享C#的字典如何返回第一个键值。

首先,分析一下这个需求,如果是输入或者List返回第一个值是很容易的,直接数组[0] 或者List[0],就可以返回第一个值,但是这个在字典中是不适用的。

比如:

using System.Collections.Generic;
using UnityEngine;

public class TestScripts : MonoBehaviour
{
    private Dictionary<int, int> dicTest;

    void Start()
    {
        dicTest = new Dictionary<int, int>();
        dicTest.Add(2, 13);
        dicTest.Add(3, 14);
        dicTest.Add(4, 15);
        dicTest.Add(0, 11);
        dicTest.Add(1, 12);
    }
}

这时候的如果Debug.Log(dicTest[0])返回是第4个键值,也就是Key值等于0的键值。

这显然与我们的要求不符,我们需要返回字典中第一个键值。

二、解决方案
2-1、使用循环返回第一个元素
找到第一个元素直接返回值:

using System.Collections.Generic;
using UnityEngine;

public class TestScripts : MonoBehaviour
{
    private Dictionary<int, int> dicTest;

    void Start()
    {
        dicTest = new Dictionary<int, int>();
        dicTest.Add(2, 13);
        dicTest.Add(3, 14);
        dicTest.Add(4, 15);
        dicTest.Add(0, 11);
        dicTest.Add(1, 12);
        Debug.Log(FirstValue());
    }

    private int FirstValue()
    {
        int value = 0;
        foreach (var item in dicTest)
        {
            value = item.Value;
            return value;
        }
        return value;
    }
}

PS:这种写法简单粗暴,在循环中直接返回第一个值,如果没有值就返回0,键值对类型变的时候修改函数即可,比如:


2-2、使用System.Linq返回第一个键值
引入using System.Linq;命名空间:

using System.Linq;
1
使用First()函数就可以返回第一个键值对了:

using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class TestScripts : MonoBehaviour
{
    private Dictionary<string, string> dicTest;

    void Start()
    {
        dicTest = new Dictionary<string, string>();
        dicTest.Add("2","13");
        dicTest.Add("0","10");
        Debug.Log(dicTest.First().Value);
    }
}

效果:

PS:Linq是使用拓展方法,遍历了集合的查询过程,使用简单方便,非常的有用。但是Linq查找的实现过程并不知道,好用是好用,但是对于代码能力的提升并不大。

2-3、使用拓展方法返回第一个键值
我们的第三个方法,就是使用拓展方法,模仿Linq返回第一个键值,参考代码:

拓展方法:

public static KeyValuePair<K, V> First<K, V>(this Dictionary<K, V> dic)
{
    KeyValuePair<K, V> dicTemp = new KeyValuePair<K, V>();
    if (dic.Count > 0)
    {
        foreach (var item in dic)
        {
            dicTemp = item;
            return dicTemp;
        }
        return dicTemp;
    }
    else
    {
        return dicTemp;
    }
}


PS:拓展方法,我已经讲过很多次了,可以翻看这篇文章:https://blog.csdn.net/q764424567/article/details/109263902

调用函数:

using System.Collections.Generic;
using UnityEngine;

public class TestScripts : MonoBehaviour
{
    private Dictionary<string, string> dicTest;

    void Start()
    {
        dicTest = new Dictionary<string, string>();
        dicTest.Add("2","13");
        dicTest.Add("0","10");
        Debug.Log(dicTest.First().Value);
    }
}

PS:没有使用Linq,但是实现跟Linq一样的效果。

三、总结
今天分享了三种方法返回第一个键值。

使用了循环,返回第一个键值
使用了Linq查询函数,返回第一个键值
使用拓展方法,模仿LInq查询,返回第一个键值

                        
原文链接:https://blog.csdn.net/q764424567/article/details/122238924

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值