C # Common algorithms: Collections

Source: Internet
Author: User

The data structure of a collection type is an important part of everyday programming, and most business requirements need to be used in the collection type of the dataset. NET platform provides us with a wide range of collection types of data structures, we need to simply call the appropriate API to complete the scattered data collation. This paper collects the present. NET platform to provide us with a small example of all the collection types.

IEnumerable

Namespace System.Collections.Generic  
{public  
    interface ienumerable<out t>: IEnumerable  
    {  
        Ienumerator<t> GetEnumerator ();  
    }  
public static void Enumerabletest ()  
        {  
            list<string> tmp = new list<string> ();  
            Tmp. ADD ("Apple");  
            Tmp. ADD ("Grape");  
            Tmp. ADD ("watermelon");  
            strenumerable = tmp as ienumerable<string>;  
            if (TMP!= null)  
            {  
                var iterator=strenumerable.getenumerator ();  
                while (iterator. MoveNext ())  
                {  
                    Console.WriteLine (iterator). current);  
                }  
      
            /*output 
             Apple 
             Grape 
             Watermelon 
             */
        }

ICollection

 namespace System.Collections.Generic {public interface icollection<t>: Ienumerable<t>, Ienumer  
        Able {bool IsReadOnly {get;}  
        void Clear ();  
        void CopyTo (t[] array, int arrayindex);  
    BOOL Remove (T item); }  
}
public static void Collectiontest ()  
        {   
            list<int> sourcetable=new list<int> ();  
            Sourcetable.add ();  
            Sourcetable.add (a);  
            Sourcetable.add ();  
            Intcollection = SourceTable as icollection<int>;  
            if (intcollection!= null)  
            {  
                Console.WriteLine ("current collection has {0} elements totally", Intcollection.count);  
                var iterator = Intcollection.getenumerator ();  
                while (iterator. MoveNext ())  
                {  
                    Console.WriteLine (iterator). current);  
                }  
      
            /*out put current 
                collection has 3 elements totally
        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.