溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-07-13 02:00:47 來源:網絡 閱讀:3834 作者:1473348968 欄目:編程語言

--------------------------------------------------------------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();
        }

    }
}

 

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女