C#如何使用集合类?C#数据结构用法介绍

2021年4月1日16:46:49 发表评论 624 次浏览

Collection<T>班级提供通用集合的基类。 T是集合中元素的类型。这个课程属于System.Collections.ObjectModel命名空间。

特点:

  • Collection<T>类可以通过创建其构造类型之一的实例立即使用。
  • Collection<T>类提供了受保护的方法, 可用于自定义其在添加和删除项目, 清除集合或设置现有项目的值时的行为。
  • 最多Collection<T>可以修改对象。但是, 使用只读初始化的Collection对象IList <T>对象无法修改。
  • 可以使用整数索引访问此集合中的元素。该集合中的索引是从零开始.
  • Collection<T>接受null作为引用类型的有效值, 并允许重复的元素。

建设者

构造器 描述
Collection<T>() 初始化为空的Collection <T>类的新实例。
Collection<T>(IList <T>) 初始化Collection <T>类的新实例, 作为指定列表的包装器。

例子:

// C# code to create a Collection
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
  
class GFG {
  
     // Driver code
     public static void Main()
     {
         // Creating a collection of ints
         Collection< int > myColl = new Collection< int >();
  
         // Adding elements in Collection myColl
         myColl.Add(2);
         myColl.Add(3);
         myColl.Add(4);
         myColl.Add(5);
  
         // Displaying the elements in myColl
         foreach ( int i in myColl)
         {
             Console.WriteLine(i);
         }
     }
}

输出如下:

2
3
4
5

属性

属性 描述
Count 获取Collection <T>中实际包含的元素数。
Items 获取围绕Collection <T>的IList <T>包装器。
Item[Int32] 获取或设置指定索引处的元素。

例子:

// C# Code to illustrate the 
// Properties of Collection class
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
   
class GFG {
   
     // Driver code
     public static void Main()
     {
          
         // Creating a collection of strings
         Collection< string > myColl = new Collection< string >();
   
         // Adding elements in Collection myColl
         myColl.Add( "A" );
         myColl.Add( "B" );
         myColl.Add( "C" );
         myColl.Add( "D" );
         myColl.Add( "E" );
          
         // ------- Count Property ----------
          
         // To print the count of
         // elements in Collection
         Console.WriteLine( "Count : " + myColl.Count);
   
         // -------- Item[Int32] Property --------
          
         // Get the element at index 2
         Console.WriteLine( "Element at index 2 is : " + myColl[2]);
   
         // Get the element at index 3
         Console.WriteLine( "Element at index 3 is : " + myColl[3]);
     }
}

输出如下:

Count : 5
Element at index 2 is : C
Element at index 3 is : D

方法

方法 描述
Add(T) 将一个对象添加到Collection <T>的末尾。
Clear() 从Collection <T>中删除所有元素。
ClearItems() 从Collection <T>中删除所有元素。
Contains(T) 确定元素是否在Collection <T>中。
CopyTo(T [], Int32) 从目标数组的指定索引开始, 将整个Collection <T>复制到兼容的一维数组。
Equals(Object) 确定指定对象是否等于当前对象。
GetEnumerator() 返回遍历Collection <T>的枚举数。
GetHashCode() 用作默认哈希函数。
GetType() 获取当前实例的类型。
IndexOf(T) 搜索指定的对象, 并返回整个Collection <T>中第一次出现的从零开始的索引。
Insert(Int32, T) 将元素插入指定索引处的Collection <T>中。
InsertItem(Int32, T) 将元素插入指定索引处的Collection中。
MemberwiseClone() 创建当前对象的浅表副本。
Remove(T) 从Collection <T>中删除第一次出现的特定对象。
RemoveAt(Int32) 删除位于Collection <T>的指定索引处的元素。
RemoveItem(Int32) 删除位于Collection <T>的指定索引处的元素。
SetItem(Int32, T) 替换指定索引处的元素。
ToString() 返回表示当前对象的字符串。

例子:

// C# code to check if an
// element is in the Collection
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
  
class GFG {
  
     // Driver code
     public static void Main()
     {
         // Creating a collection of strings
         Collection< string > myColl = new Collection< string >();
  
         myColl.Add( "A" );
         myColl.Add( "B" );
         myColl.Add( "C" );
         myColl.Add( "D" );
         myColl.Add( "E" );
  
         // Checking if an element is in the Collection
         // The function returns "True" if the
         // item is present in Collection
         // else returns "False"
         Console.WriteLine(myColl.Contains( "A" ));
     }
}

输出如下:

True

范例2:

// C# code to copy the entire Collection
// to a compatible one-dimensional Array, // starting at the specified index of
// the target array
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
  
class GFG {
  
     // Driver code
     public static void Main()
     {
         // Creating a collection of strings
         Collection< string > myColl = new Collection< string >();
  
         myColl.Add( "A" );
         myColl.Add( "B" );
         myColl.Add( "C" );
         myColl.Add( "D" );
         myColl.Add( "E" );
  
         // Creating a string array
         string [] myArr = new string [myColl.Count];
  
         // Copying the entire Collection to a
         // compatible one-dimensional Array, // starting at the specified index
         // of the target array
         myColl.CopyTo(myArr, 0);
  
         // Displaying the elements in myArr
         foreach ( string str in myArr)
         {
             Console.WriteLine(str);
         }
     }
}

输出如下:

A
B
C
D
E

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1?view=netframework-4.7.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: