Csharp/CSharp Tutorial/Data Structure/ICollection — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Collections interoperability: cast Array to IList, IList to ICollection, ICollection to IEnumerable, non-generic to generic

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;
    }
}

ICollection<T>: add a few elements to the collection

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);
    }
}

ICollection<T>: Copy the contents to an array to iterate it

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]);
    }
}
105
232
350

ICollection<T>: delete one

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);
    }
}

ICollection<T>: Enumerate the collection"s contents

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);
    }
}
105
232
350

ICollection<T>: Search for some specific elements

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));
    }
}
Contains 105? True
Contains 232? True