SortedList<TKey, TValue>使用示例

SortedList<TKey, TValue>表示键/值对的集合,并且各个键/值对自动按照键进行排序,示例如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<string, int> list = new SortedList<string, int>();
            list.Add("d", 4);
            list.Add("c", 3);
            list.Add("a", 1);
            list.Add("b", 2);

            foreach (KeyValuePair<string, int> item in list)
            {
                Console.WriteLine(item.Key + "," + item.Value.ToString());
            }

            Console.ReadKey();
        }
    }
}

输出如下:

引用内容 引用内容
a,1
b,2
c,3
d,4


评论: 0 | 引用: 0 | 查看次数: 4320
发表评论
登录后再发表评论!