C#索引值對容器的介紹

來源:互聯網
上載者:User

StringDictionary:預設key不區分大小寫
NameValueCollection:預設key區分大小寫
KeyedCollection:不是索引值對容器,但是比索引值對容器更好用,強烈推薦

命名空間using System.Collections.Specialized

System.Collections 命名空間包含介面和類,這些介面和類定義各種對象(如列表、隊列、位元組、雜湊表和字典)的集合。
System.Collections.Generic 命名空間包含定義泛型集合的介面和類,泛型集合允許使用者建立強型別集合,它能提供比非泛型強型別集合更好的型別安全和效能。
System.Collections.Specialized 命名空間包含專用的和強型別的集合,例如,連結的列表詞典、位向量以及只包含字串的集合。

Hashtable、SortedList
SortedList為可排序的字典,當添加元素時,元素按照正確的排序次序插入SortedList,同時索引自動進行相應的調整,移除元素亦然。
Hashtable、SortedList的鍵和值均為object類型,因此使用的時候,轉化比較頻繁

dictionary
範型Dictionary,可以隨便制定key,value的類型

複製代碼 代碼如下:Dictionary <String, String> dic = new Dictionary <string, string> ();
dic.Add( "1 ", "Jerry ");
dic.Add( "2 ", "Kimmy ");
dic.Add( "3 ", "Tommy ");

也可以自己定義類來使用

複製代碼 代碼如下:public class KeyValueItem
{
private int _Value;
public int Value
{
get
{
return _Value;
}
}
private string _Name;
public string Name
{
get
{
return _Name;
}
}
//
public KeyValueItem(string name, int value)
{
_Name = name;
_Value = http://www.jb51.net/dgjack/archive/2012/03/03/value;
}
public override string ToString()
{
return _Name;
}
}

插入值的時候:複製代碼 代碼如下:KeyValueItem it = new KeyValueItem("客戶1", 1);
comboBox1.Items.Add(it);
it = new KeyValueItem("客戶2", 2);
comboBox1.Items.Add(it);
it = new KeyValueItem("客戶3", 3);
comboBox1.Items.Add(it);

取值的時候就用 :複製代碼 代碼如下:int relationtype = ((KeyValueItem)comboBox1.SelectedItem).Value;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.