一.基础篇
1.Dictionary泛型类提供了从一组键到一组值的映射,即键和值的集合类。
2.Dictionary通过键来检索值的速度是非常快的,这是因为 Dictionary 类是作为一个哈希表来实现的。
3.定义方式:
Dictionary<[Key], [Value]> openWith = new Dictionary<[Key], [Value]>();
其中:Key代表此泛型类的键,不可重复。
Value代表此泛型类中键对应的值。
Key和Value可以用int,decimal,float等值类型,也可以用string,class等引用类型。
举例:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Test(); } public void Test() { //Key为值类型 Value为值类型 Dictionary<int, int> dicInt = new Dictionary<int, int>(); //Key为值类型 Value为引用类型 Dictionary<int, string> dicString = new Dictionary<int, string>(); //Key为引用类型 Value为引用类型 Dictionary<TestInfo, TestInfo> dicTestClass = new Dictionary<TestInfo, TestInfo>(); } } public class TestInfo { public string ID { get; set; } public string Name { get; set; } public string Pwd { get; set; } } }