C#基础(003)---泛型与数组1

C#基础---泛型与数组1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int[] arr = { 0, 1, 2, 3, 4 };// int型数组
            string[] str = { "0a", "1b", "2c", "3d", "4e" }; //string型数组
            List<int> list = new List<int>(); //泛型数组

            for (int x = 5; x < 10; x++)
            {
                list.Add(x);
            } 
           
            ProcessItems<int>(arr);          
          
            ProcessItems<int>(list);

            ProcessItems<string >(str);
            Console.ReadLine();
        }

        static void ProcessItems<T>(IList<T> coll)
        {
            // IsReadOnly returns True for the array and False for the List.
            System.Console.WriteLine
                ("IsReadOnly returns {0} for this collection.",
                coll.IsReadOnly);        

            foreach (T item in coll)
            {
                System.Console.Write(item.ToString() + " ");
               
            }           
            System.Console.WriteLine();         
            
        }
       
    }

}

输出:

 

posted on 2014-04-17 23:10  lbsf  阅读(173)  评论(0)    收藏  举报

导航