1 using System; 2 using System.Collections.Generic; 3 4 public class ListTest 5 { 6 public static void Main() 7 { 8 List<string> fruits = new List<string>(); 9 10 fruits.Add("Apple"); 11 fruits.Add("Banana"); 12 fruits.Add("Carrot"); 13 14 Console.WriteLine( "Count: {0}", fruits.Count ); 15 16 PrintValues1( fruits ); 17 PrintValues2( fruits ); 18 PrintValues3( fruits ); 19 } 20 21 //List实现了IList接口 22 static void PrintValues1( IList<string> myList ) 23 { 24 for(int i=0; i<myList.Count; i++ ) 25 Console.Write( "{0}\n", myList[i] ); 26 } 27 28 static void PrintValues2( IList<string> myList ) 29 { 30 foreach( string item in myList ) 31 Console.Write( "{0}\n", item ); 32 } 33 34 static void PrintValues3( IEnumerable<string> myList ) 35 { 36 IEnumerator<string> myEnumerator = myList.GetEnumerator(); 37 while ( myEnumerator.MoveNext() ) 38 Console.Write( "{0}\n", myEnumerator.Current ); 39 Console.WriteLine(); 40 } 41 }