Using the ICollection Interface in C#

using System; 
using System. Collections. Generic; 
 
namespace w3m { 
 class w3m { 
        static void Main(string[] args) { 
 
            // create a new collection 
            ICollection<string> coll = new List<string>() ; 
 
            // add some items 
            coll. Add("dogs") ; 
            coll. Add("cats"); 
 
            // check to see if a value is in the collection 
            Console.WriteLine("Contains ' dogs' : {0}", coll.Contains("dogs") ); 
            Console.WriteLine("Contains ' cats' : {0}", coll. Contains("cats") ); 
 
            // copy to an array  
            string[ ] arr = new string[coll. Count] ; 
            coll. CopyTo(arr, 0) ; 
 
            // enumerate the collection contents 
            foreach (string str in coll) { 
                Console.WriteLine("Collection item: {0}", str); 
            } 
 
            // print out the collection count  
            Console.WriteLine("Count: {0}", coll. Count) ; 
 
            // clear the contents of the collection 
            coll. Clear(); 
 
            // wait for input before exiting
            Console.WriteLine("Press enter to finish"); 
            Console.ReadLine(); 
        } 
    } 
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.