C#专用集合类命名空间:System.Collections.Specialized
System.Collections.Specialized 命名空间包含专用的和强类型的集合。
StringCollection 和 StringDictionary ,两者都包含独占字符串的值。
StringCollection 我们可以认为是类似于 List<String> 或者string[] 数组。
StringDictionary 我们可以认为是类似于 Dictionary<string, string> 的键值对集合。
StringCollection 类
表示字符串的集合。
StringCollection 接受 null
作为有效值,并允许重复元素。
字符串比较区分大小写。
可以使用整数索引访问此集合中的元素。 此集合中的索引从零开始。
StringDictionary 类
StringDictionary使用字符串(而不是对象)强类型的键和值来实现哈希表。
键不能为 null
,但值可以为null。
以不区分大小写的方式进行处理;它在与字符串字典一起使用之前转换为小写形式。键不区分大小写
键不能重复【"ABC"和"abc"认为是同一个键】
两者比较:
类别 | 是否区分大小写 | 是否允许键值重复 | 访问方式 |
StringCollection | True | True | 通过索引Index来访问 |
StringDictionary | False | False | 通过键key来访问 |
创建控制台应用程序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元素区分大小写,允许重复,允许为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不能添加重复的键,键不区分大小写.\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}】");
}
}
}
}
运行效果