C#专用集合类StringCollection与StringDictionary

83 篇文章 3 订阅

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 TrueTrue通过索引Index来访问
StringDictionaryFalseFalse通过键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}】");
            }
        }
    }
}
 

运行效果

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯内科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值