《随笔三十六》——C#中的 “ 解析 泛型 “ HashSet<T> 类 中的属性”

本文介绍了C#中HashSet<T>类的构造函数、属性和方法,包括添加、删除、检查元素是否存在、集合操作等功能。HashSet<T>表示值的集合,类型参数T表示集合中元素的类型。文章详细解析了如Comparer、Count属性及Add、Remove、IntersectWith等方法的工作原理和性能特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

HashSet Constructors

 Propertys

HashSet.Comparer

HashSet.Count

Methods

HashSet.Add

HashSet.Clear

HashSet.Contains

HashSet.CopyTo

HashSet.ExceptWith

HashSet.IntersectWith

HashSet.IsProperSubsetOf

HashSet.IsProperSupersetOf

HashSet.IsSubsetOf

HashSet.IsSupersetOf

HashSet.Overlaps

HashSet.Remove

HashSet.SetEquals

HashSet.SymmetricExceptWith

HashSet.TrimExcess 

HashSet.TryGetValue

HashSet.UnionWith


该类在 System.Collections.Generic 命名空间。

点击这里进入官网了解更多成员详情

HashSet<T> 类 表示值的集。

类型参数 T 表示哈希集中的元素类型。


HashSet<T> Constructors


该构造函数还有很多的重载形式 。这里列出常用的三种形式

public HashSet ();
public HashSet (System.Collections.Generic.IEnumerable<T> collection);
public HashSet (int capacity);
        static void Main(string[] args)
        {
            // 调用默认构造函数,该实例为空并使用集类型的默认相等比较器
            HashSet<int> evenNumbers = new HashSet<int>();

            int[] number = { 55,55, 44, 33 ,33};
            HashSet<int> evenNumbers1 = new HashSet<int>(number);
            foreach(var tt in evenNumbers1)
            {
                WriteLine(tt); // 输出 55 44 33
            }
            WriteLine();
            HashSet<int> evenNumber2 = new HashSet<int>(evenNumbers1); // evenNumber2 是 evenNumbers1 的副本
            foreach (var tt in evenNumber2)
            { 
                WriteLine(tt); // 输出 55 44 33
            }

            HashSet<int> evenNumber3 = new HashSet<int>(10); // evenNumber3 的初始容量是10

        }

 Propertys


HashSet<T>.Comparer

public System.Collections.Generic.IEqualityComparer<T> Comparer { get; }

HashSet<T>.Count

public int Count { get; }
  • 获取集中包含的元素数。
        static void Main(string[] args)
        {           
            int[] number = { 55,55, 44, 33 ,33};
            HashSet<int> evenNumbers1 = new HashSet<int>(number);
            // 输出3, 记住 HashSet 中的元素值不能是重复的,每一个元素值必须是唯一的
            WriteLine(evenNumbers1.Count); 
        }

Methods


HashSet<T>.Add

public bool Add (T item);
  • 将名为 item 的元素添加到HashSet<T> 的尾部。
  • 如果将元素添加到 HashSet <T> 对象中,则为true; 如果该元素已存在,则返回 false。
  • 如果 Count已经等于 HashSet <T> 对象的当前容量,则会自动调整容量以容纳新的 item 元素。
  • 如果Count小于内部数组的当前容量,则此方法为 o (1) 操作。 如果HashSet<T>对象必须调整大小,此方法将成为 O (n) 操作,其中nCount
            int[] number = { 55,55, 44, 33 ,33};
            HashSet<int> evenNumbers1 = new HashSet<int>(number);
            // 输出3, 记住 HashSet 中的元素值不能是重复的,每一个元素值必须是唯一的
            evenNumbers1.Add(100);
            evenNumbers1.Add(200);
          foreach(var tt in evenNumbers1)
            {
                WriteLine(tt);
            }

HashSet<T>.Clear

public void Clear ();
  • 从 HashSet<T> 对象中移除所有元素。并释放该集合所引用的内存。
  • 此方法为 O (n) 操作,其中nCount

HashSet<T>.Contains

public bool Contains (T item);
  • 确定 HashSet<T> 对象是否包含名为 item 的元素。
  • 如果 HashSet<T> 对象包含名为 item 的元素,则为 true;否则为 false
  • 此方法为 o (1) 运算。
        static void Main(string[] args)
        {           
            int[] number = { 55,55, 44, 33 ,33};
            HashSet<int> evenNumbers1 = new HashSet<int>(number);           
            evenNumbers1.Add(100);
            evenNumbers1.Add(200);   
           if(evenNumbers1.Contains(55))
            {
                WriteLine("包含");
            }
           else
                WriteLine("不包含");
        }

HashSet<T>.CopyTo

public void CopyTo (T[] array);
  • 将当前的 HashSet<T> 对象的元素复制到一个名为 array 的T 类型的一维数组中。
  • 如果参数 array 为 null。抛出  ArgumentNullException
  • 此方法为 O (n) 操作,其中n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值