C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# SortedDictionary<TKey, TValue>

C# SortedDictionary<TKey, TValue>

C# SortedDictionary<TKey, TValue> 类使用了哈希表的概念。它根据键存储值。它包含唯一键并在键的基础上保持升序。通过键的帮助,我们可以轻松地搜索或删除元素。它位于 System.Collections.Generic 命名空间中。

C# SortedDictionary<TKey, TValue> 示例

让我们看一个使用 Add() 方法存储元素并迭代的通用 SortedDictionary<TKey, TValue> 类的示例元素使用 for-each 循环。在这里,我们使用 KeyValuePair 类来获取键和值。
 
using System;
using System.Collections.Generic;
public class SortedDictionaryExample
{
    public static void Main(string[] args)
    {
        SortedDictionary<string, string> names = new SortedDictionary<string, string>();
        names.Add("1","Sonoo");  
        names.Add("4","Peter");  
        names.Add("5","James");  
        names.Add("3","Ratan");  
        names.Add("2","Irfan");  
        foreach (KeyValuePair<string, string> kv in names)
        {
            Console.WriteLine(kv.Key+" "+kv.Value);
        }
    }
}
输出:
 1 Sonoo
2 Irfan
3 Ratan
4 Peter
5 James
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4