码迷,mamicode.com
首页 > 其他好文 > 详细

List

时间:2019-03-19 15:11:18      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:generic   ons   ring   new   stat   \n   item   void   console   

 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 }

 

List

标签:generic   ons   ring   new   stat   \n   item   void   console   

原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10558588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!