字典---有序字典(SortedDictionary<TKey,TValue>)
--------------------------------------------------------------EmployeeID.cs(键)
创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站建设、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元南和做网站,已为上家服务,为南和各地企业和个人服务,联系电话:18982081108
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; public class EmployeeID : IComparable{ 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使用的内存少 //SortedDictionary 元素插入和删除速度快 //键要实现IComparable 接口 SortedDictionary sd = new SortedDictionary (); 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(); } } }
文章标题:字典---有序字典(SortedDictionary<TKey,TValue>)
文章路径:http://pwwzsj.com/article/pcsded.html