/* 属性 */ AllKeys; //返回包含所有键的 string[] Count; // Keys; //键集合 /* 方法 */ Add(); // Clear(); // CopyTo(); // Get(); //根据索引或键取值, 有多个值时会用逗号隔开 GetKey(); //根据索引取键 GetValues(); //根据索引或键取值, 返回 string[] HasKeys(); //是否包含非 null 键 Remove(); //根据键移除 Set(); //改值; 若键不存在则同 Add()
protected void Button1_Click(object sender, EventArgs e) { NameValueCollection nv = new NameValueCollection(); nv.Add("k1", "AAA"); nv.Add("k2", "BBB"); nv.Add("k3", "CCC"); nv.Add(null, "DDD"); nv.Add("k2", "b"); nv.Add("k2", "bb"); nv.Add("k2", "bbb"); string str1 = nv["k1"]; //AAA string str2 = nv["k2"]; //BBB,b,bb,bbb : 多个值会用逗号分开 string str3 = nv[1]; //BBB,b,bb,bbb string str4 = nv[null]; //DDD TextBox1.Text = str1 + "\n" + str2 + "\n" + str3 + "\n" + str4; }
protected void Button1_Click(object sender, EventArgs e) { NameValueCollection nv = new NameValueCollection(); nv.Add("k1", "AAA"); nv.Add("k2", "BBB"); nv.Add("k3", "CCC"); nv.Add("k2", "b"); nv.Add("k2", "bb"); nv.Add("k2", "bbb"); int n = nv.Count; //3 : k1、k2、k3 nv.Set("k1", "aaa"); string str1 = nv.Get(0); //aaa string str2 = nv.Get("k1"); //aaa string str3 = nv.GetKey(0); //k1 string[] sArr1 = nv.GetValues(0); string[] sArr2 = nv.GetValues("k2"); string str4 = string.Join("; ", sArr1); //aaa string str5 = string.Join("; ", sArr2); TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}", n, str1, str2, str3, str4, str5); } protected void Button2_Click(object sender, EventArgs e) { NameValueCollection nv = new NameValueCollection(); nv.Add("k1", "AAA"); nv.Add("k2", "BBB"); nv.Add("k3", "CCC"); nv.Add("k2", "b"); nv.Add("k2", "bb"); nv.Add("k2", "bbb"); NameValueCollection.KeysCollection keys = nv.Keys; string str1 = ""; foreach (string s in keys) { str1 += s + ", "; } //k1, k2, k3, string str2 = string.Join("; ", nv.AllKeys); //k1; k2; k3 TextBox1.Text = str1 + "\n" + str2; }