字典---有序字典(SortedDictionary<TKey,TValue>)

发布时间:2020-07-13 02:00:47 作者:1473348968
来源:网络 阅读:3799

--------------------------------------------------------------EmployeeID.cs(键)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
public class EmployeeID : IComparable<EmployeeID>
{
    public int ID { get; private set; }
    public EmployeeID(int id)
    {
        this.ID = id;
    }
   
    public int CompareTo(EmployeeID other)
    {
        return ID.CompareTo(other.ID);
    }
}

 --------------------------------------------------------------Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    public class Person
    {
        public string Country { get; private set; }
        public string Name { get; private set; }
        public int Age { get; private set; }
        public Person(string country, string name, int age)
        {
            this.Country = country;
            this.Name = name;
            this.Age = age;
        }
       
    }
}

 --------------------------------------------------------------主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            //SortedList<TKey,TValue>使用的内存少
            //SortedDictionary<TKey,TValue>元素插入和删除速度快
            //键要实现IComparable<in T>接口
            SortedDictionary<EmployeeID, Person> sd = new SortedDictionary<EmployeeID, Person>();
            sd.Add(new EmployeeID(3),new Person("中国", "张飞", 40));
            sd.Add(new EmployeeID(20), new Person("中国", "关羽", 43));
            sd.Add(new EmployeeID(4), new Person("中国", "刘备", 45));
            sd.Add(new EmployeeID(5), new Person("中国", "诸葛亮", 24));
            sd.Add(new EmployeeID(1), new Person("美国", "豪威尔", 40));
            sd.Add(new EmployeeID(0),new Person("美国", "奥巴马", 40));
            sd.Add(new EmployeeID(210), new Person("朝鲜", "金三胖", 40));
            sd.Add(new EmployeeID(80), new Person("印度", "印度阿三", 40));
            
            foreach (var item in sd)
            {
                Console.WriteLine(item.Key.ID+","+item.Value.Name);//键,正序排序
            }
            Console.ReadKey();
        }

    }
}

 

推荐阅读:
  1. python有序字典
  2. IDictionary<TKey,TValue>数据字典使用讲解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

private return public

上一篇:微软软件定义网络HNV PA和CA地址详述

下一篇:c#开发ocx

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》