好吧,先讓再沮喪一會兒,然後再開始這個文章
昨天終於從emule上把whidbey alpha拉下來了,2.6G啊,今天裝上後,迫不及待地試了一下C#中的泛型,然,我要哭! 這功能也太弱了吧,才多久啊,C#的泛型就沒新鮮東西可玩了,這對於從C++轉到C#的我來說,這哪裡能稱得上泛型啊!!除了做個容器(呼!即便做container也不見得方便)一無用處啊。當初在msdn上看到那個泛型的constraint就覺得不妙,果不其然。號稱從clr支援泛型,然實作出來的東西,功能上一點也不見得比java高明到哪裡去(效率是C#泛型要高很多,具體的我下次再寫吧)。最誇張的是執行個體化的類型竟然必須有一個無參建構函式,暈啊!
sigh..... Bruce Eckel的話還真是中肯啊
太傷心了,寫不下去了,先把我的第一個C#泛型的實驗程式帖出來吧,回頭我會寫一篇詳細的 Take a Close Look at C# Generics
using System;
namespace ConsoleApplication1
{
interface ITest
{
void testMethod();
}
class testType : ITest
{
//public testType(int i) {}
public void testMethod()
{
Console.WriteLine("testing");
}
}
class test<T> where T:ITest,new()
{
public test(int i)
{
dataT = new T();
dataInt = i;
}
public void foo()
{
dataT.testMethod();
}
public T DataT
{
get { return dataT; }
}
public int DataInt
{
get { return dataInt; }
}
private T dataT;
private int dataInt;
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
test<testType> ti = new test<testType>(4);
//Console.WriteLine(ti.DataT);
ti.foo();
Console.WriteLine(ti.DataInt);
}
}
}