C#应用 - 04.怎么按下标获取Dictionary的元素

  1. 按下标获取Dictionary的元素主要利用了查询表达式LINQ的扩展方法。
using System;
using System.Collections.Generic;
using System.Linq;

namespace _04
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Dictionary<object, object> dictionary = new Dictionary<object, object>();
            dictionary.Add(1, "c");
            dictionary.Add(3, "a");
            dictionary.Add(2, "b");
            for (int i = 0; i < dictionary.Count; i++)
            {
    
    
                Console.WriteLine($"{
      
      dictionary.ElementAt(i).Key}\t{
      
      dictionary.ElementAt(i).Value}");
            }
        }
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44021223/article/details/121446517