using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList myList = new ArrayList(20);
myList.Add(20); //装箱操作.
myList.Add(202);
int i1 = (int)myList[0]; //拆箱操作
foreach (int item in myList)
{
Console.WriteLine(item); //这个程序是关于ArrayList的.
}
List<int> myListT = new List<int>(10);
myListT.Add(412); //不会装箱操作
int i2 = myListT[0]; //不会拆箱.
foreach (int item in myListT)
{
Console.WriteLine(item);
}
//值得注意的是,如果在附值的时候有不同类型进去,但是用foreach循环的时候就会报错,
//用泛型可以避免这种情况。在编译的时候就会报错.
}
public class MyOK //下面是泛型的几个例子
{
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
public delegate TOutput Converter<TOutput, TIntput>(TIntput from);
}
public class SortedList<Tkey, Tvalue>
{
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
//以下是另外一个例子。关于List<T>的FindAll方法和Find//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace 泛型
{
public class Racer
{
private string name;
public string Name
{
get { return name; }
}
private string car;
public string Car
{
get { return car; }
}
public Racer(string name,string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return string.Format("Name:{0,-20}Car:{1}", Name, Car);
}
}
public class MainRun
{
static void Main()
{
List<Racer> racers = new List<Racer>();
racers.Add(new Racer("张三","奥迪"));
racers.Add(new Racer("李四", "烂车子"));
//foreach (Racer item in racers)
//{
// Console.WriteLine(item);
//}
FindRacer finder = new FindRacer("奥迪");
foreach (Racer item in racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate))) //这是泛型的List的FindAll.
{
Console.WriteLine(item);
}
}
}
public class FindRacer
{
private string car;
public FindRacer(string car)
{
this.car = car;
}
public bool DrivingCarPredicate(Racer racer)
{
return racer.Car == car;
}
}
}