Csharp/CSharp Tutorial/Data Structure/ICollection
Содержание
- 1 Collections interoperability: cast Array to IList, IList to ICollection, ICollection to IEnumerable, non-generic to generic
- 2 ICollection<T>: add a few elements to the collection
- 3 ICollection<T>: Copy the contents to an array to iterate it
- 4 ICollection<T>: delete one
- 5 ICollection<T>: Enumerate the collection"s contents
- 6 ICollection<T>: Search for some specific elements
Collections interoperability: cast Array to IList, IList to ICollection, ICollection to IEnumerable, non-generic to generic
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { int[] arrayOfInts = new int[] { 10, 99, 50 }; Array a = (Array)arrayOfInts; IList list = (IList)arrayOfInts; ICollection collection = (ICollection)arrayOfInts; IEnumerable enumerable = (IEnumerable)arrayOfInts; ICollection<int> collectionGeneric = (ICollection<int>)arrayOfInts; IEnumerable<int> enumerableGeneric = (IEnumerable<int>)arrayOfInts; }
}</source>
ICollection<T>: add a few elements to the collection
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { ICollection<int> myCollection = new Collection<int>(); myCollection.Add(105); myCollection.Add(232); myCollection.Add(350); }
}</source>
ICollection<T>: Copy the contents to an array to iterate it
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { ICollection<int> myCollection = new Collection<int>(); myCollection.Add(105); myCollection.Add(232); myCollection.Add(350); int[] myArray = new int[myCollection.Count]; myCollection.CopyTo(myArray, 0); for (int i = 0; i < myArray.Length; i++) Console.WriteLine(myArray[i]); }
}</source>
105 232 350
ICollection<T>: delete one
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { ICollection<int> myCollection = new Collection<int>(); myCollection.Add(105); myCollection.Add(232); myCollection.Add(350); myCollection.Remove(232); }
}</source>
ICollection<T>: Enumerate the collection"s contents
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { ICollection<int> myCollection = new Collection<int>(); myCollection.Add(105); myCollection.Add(232); myCollection.Add(350); foreach (int i in myCollection) Console.WriteLine(i); }
}</source>
105 232 350
ICollection<T>: Search for some specific elements
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { ICollection<int> myCollection = new Collection<int>(); myCollection.Add(105); myCollection.Add(232); myCollection.Add(350); Console.WriteLine("Contains {0}? {1}", 105, myCollection.Contains(105)); Console.WriteLine("Contains {0}? {1}", 232, myCollection.Contains(232)); }
}</source>
Contains 105? True Contains 232? True