對Dictionary操作 尋找項和排序 [代碼記錄]

來源:互聯網
上載者:User
KeyCollection:
  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               EmployeeCollection employees =
               new EmployeeCollection();
                employees.Add(new Employee(1, "Joe"));
                employees.Add(new Employee(2, "Jim"));
                employees.Add(new Employee(3, "Jane"));
                if (employees.Contains(3))
                {
                    Employee emp = employees[3];
                    Console.WriteLine(
                        "Employee ID={0}, Name={1}",
                        emp.ID, emp.Name);
                }
                Console.ReadLine();
            }
        }
internal class Employee
        {
            public readonly int ID;
            public string Name;

            public Employee(int id, string name)
            {
                this.ID = id;
                this.Name = name;
            }
        }

        internal class EmployeeCollection : KeyedCollection<Int32, Employee>
        {
            protected override int GetKeyForItem(Employee item)
            {
                return item.ID;
            }
        } 1GroupDictionary:
 2     internal class GroupDictionary<T> : Dictionary<String, List<T>>
 3        {
 4            public void Add(String key, T value)
 5            {
 6                List<T> list = null;
 7                if (!this.ContainsKey(key) || !(this.TryGetValue(key, out list)))
 8                {
 9                    list = new List<T>();
10                    this.Add(key, list);
11                }
12                list.Add(value);
13            }
14        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.