C#专用集合类StringCollection与StringDictionary
C#專用集合類命名空間:System.Collections.Specialized
System.Collections.Specialized?命名空間包含專用的和強類型的集合。
?StringCollection?和?StringDictionary?,兩者都包含獨占字符串的值。
StringCollection 我們可以認為是類似于 List<String> 或者string[] 數(shù)組。
StringDictionary 我們可以認為是類似于 Dictionary<string, string> 的鍵值對集合。
StringCollection 類
表示字符串的集合。
StringCollection 接受?null?作為有效值,并允許重復元素。
字符串比較區(qū)分大小寫。
可以使用整數(shù)索引訪問此集合中的元素。?此集合中的索引從零開始。
StringDictionary 類
StringDictionary使用字符串(而不是對象)強類型的鍵和值來實現(xiàn)哈希表。
鍵不能為?null?,但值可以為null。
以不區(qū)分大小寫的方式進行處理;它在與字符串字典一起使用之前轉(zhuǎn)換為小寫形式。鍵不區(qū)分大小寫
鍵不能重復【"ABC"和"abc"認為是同一個鍵】
兩者比較:
| 類別 | 是否區(qū)分大小寫 | 是否允許鍵值重復 | 訪問方式 |
| StringCollection? | True | True | 通過索引Index來訪問 |
| StringDictionary | False | False | 通過鍵key來訪問 |
創(chuàng)建控制臺應用程序SpecialCollectionDemo
測試示例:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpecialCollectionDemo
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Console.SetWindowSize(100, 35);
? ? ? ? ? ? TestStringCollection();
? ? ? ? ? ? Console.WriteLine("---------------------下面測試StringDictionary---------------------");
? ? ? ? ? ? TestStringDictionary();
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 測試字符串集合
? ? ? ? /// </summary>
? ? ? ? static void TestStringCollection()
? ? ? ? {
? ? ? ? ? ? StringCollection stringCollection = new StringCollection();
? ? ? ? ? ? Console.WriteLine($"StringCollection是否同步訪問(線程安全):【{stringCollection.IsSynchronized}】");
? ? ? ? ? ? stringCollection.AddRange(new string[] { "云無月", "岑纓", "ABC", null, "北洛", "ABC" });
? ? ? ? ? ? for (int i = 0; i < stringCollection.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"當前索引:{i},元素【{stringCollection[i]}】");
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine($"StringCollection元素區(qū)分大小寫,允許重復,允許為null.是否存在元素abc【{stringCollection.Contains("abc")}】");
? ? ? ? ? ? Console.WriteLine("使用迭代器遍歷...");
? ? ? ? ? ? stringCollection.Remove("ABC");
? ? ? ? ? ? stringCollection.Remove("Test");
? ? ? ? ? ? StringEnumerator stringEnumerator = stringCollection.GetEnumerator();
? ? ? ? ? ? while (stringEnumerator.MoveNext())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(stringEnumerator.Current);
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("使用foreach遍歷...");
? ? ? ? ? ? stringCollection.RemoveAt(2);
? ? ? ? ? ? stringCollection.Insert(3, "嫘祖");
? ? ? ? ? ? foreach (string item in stringCollection)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(item);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 測試字符串字典
? ? ? ? /// </summary>
? ? ? ? static void TestStringDictionary()
? ? ? ? {
? ? ? ? ? ? StringDictionary stringDictionary = new StringDictionary();
? ? ? ? ? ? Console.WriteLine($"StringDictionary是否同步訪問(線程安全):【{stringDictionary.IsSynchronized}】");
? ? ? ? ? ? stringDictionary.Add("ABC", null);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stringDictionary.Add(null, "Value");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"StringDictionary鍵不能為null,值可以為null。\n異常信息:{ex.Message}");
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stringDictionary.Add("abc", "張三");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"StringDictionary不能添加重復的鍵,鍵不區(qū)分大小寫.\n異常信息:{ex.Message}");
? ? ? ? ? ? }
? ? ? ? ? ? stringDictionary["Abc"] = "李四";
? ? ? ? ? ? stringDictionary.Add("XYZ", "王五");
? ? ? ? ? ? stringDictionary["Hello"] = "World";
? ? ? ? ? ? foreach (System.Collections.DictionaryEntry dictionaryEntry in stringDictionary)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"鍵【{dictionaryEntry.Key}】,值【{dictionaryEntry.Value}】");
? ? ? ? ? ? }
? ? ? ? ? ? stringDictionary.Remove("HELLO");
? ? ? ? ? ? Console.WriteLine("移除指定的Hello鍵后,此時集合...");
? ? ? ? ? ? System.Collections.IEnumerator ts = stringDictionary.GetEnumerator();
? ? ? ? ? ? while (ts.MoveNext())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.Collections.DictionaryEntry dictionaryEntry = (System.Collections.DictionaryEntry)ts.Current;
? ? ? ? ? ? ? ? Console.WriteLine($"鍵【{dictionaryEntry.Key}】,值【{dictionaryEntry.Value}】");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
運行效果
?
?
總結
以上是生活随笔為你收集整理的C#专用集合类StringCollection与StringDictionary的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【python】自动登录51cto家园
- 下一篇: C#窗体调用地图(高德地图)-实现公交线