📜  C#|收集类

📅  最后修改于: 2021-05-29 19:30:12             🧑  作者: Mango

Collection < T > Class提供通用集合的基类。 T是集合中元素的类型。此类位于System.Collections.ObjectModel命名空间下。

特征:

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

建设者

Constructor Description
Collection<T>() Initializes a new instance of the Collection<T> class that is empty.
Collection<T>(IList<T>) Initializes a new instance of the Collection<T> class as a wrapper for the specified list.

例子:

// 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 myColl = new Collection();
  
        // 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

特性

Property Description
Count Gets the number of elements actually contained in the Collection<T>.
Items Gets a IList<T> wrapper around the Collection<T>.
Item[Int32] Gets or sets the element at the specified index.

例子:

// 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 myColl = new Collection();
   
        // 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

方法

Method Description
Add(T) Adds an object to the end of the Collection<T>.
Clear() Removes all elements from the Collection<T>.
ClearItems() Removes all elements from the Collection<T>.
Contains(T) Determines whether an element is in the Collection<T>.
CopyTo(T[], Int32) Copies the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an enumerator that iterates through the Collection<T>.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
IndexOf(T) Searches for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>.
Insert(Int32, T) Inserts an element into the Collection<T> at the specified index.
InsertItem(Int32, T) Inserts an element into the Collection at the specified index.
MemberwiseClone() Creates a shallow copy of the current Object.
Remove(T) Removes the first occurrence of a specific object from the Collection<T>.
RemoveAt(Int32) Removes the element at the specified index of the Collection<T>.
RemoveItem(Int32) Removes the element at the specified index of the Collection<T>.
SetItem(Int32, T) Replaces the element at the specified index.
ToString() Returns a string that represents the current object.

例子:

// 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 myColl = new Collection();
  
        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 myColl = new Collection();
  
        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/zh-cn/dotnet/api/system.collections.objectmodel.collection-1?view=netframework-4.7.2