Example of IList in Generic Lists C#

using System; 
using System.Collections.Generic; 
 
namespace w3example { 
    class w3example { 
        static void Main(string[] args) { 
 
            // create a new list 
            IList<string> list = new List<string>() ; 
 
            // add new items with the Add method 
            list.Add("item1") ; 
            list.Add("item2"); 
            list.Add("item3"); 
 
            // use the indexer to set the third value 
            list[ 2] = "item3changed"; 
 
            // insert an item at the head of the list 
            list.Insert(0, "item0"); 
 						// remove an item 
            list.RemoveAt(2); 
 
            // enumerate the items in the list 
            foreach (string str in list) { 
                Console.WriteLine("List item: {0}", str); 
            } 
 
            // check for items in the list 
            Console.WriteLine("Index of {0}: {1}", "item1",  
                list.IndexOf("item1") ); 
            Console.WriteLine("Index of {0}: {1}", "item2",  
                list.IndexOf("item2") ); 
 
            // 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.