下面这段代码为什么无法编译?
using System;
using System.Collections.Generic;
using System.Drawing;
namespace GenicTest
{
class Program
{
static void Main(string[] args)
{
List<Point> pl = new List<Point>();
pl.Add(new Point(10, 10));
pl[0].X = 42; // <-- compiler error occurs here!
Console.WriteLine(pl[0]);
}
}
}
using System.Collections.Generic;
using System.Drawing;
namespace GenicTest
{
class Program
{
static void Main(string[] args)
{
List<Point> pl = new List<Point>();
pl.Add(new Point(10, 10));
pl[0].X = 42; // <-- compiler error occurs here!
Console.WriteLine(pl[0]);
}
}
}
如果你对C++很熟的话, 你再试一试用标准C++ 中的vector来代替List
这个例子其实反应了net2.0泛型的又一个让人失望的地方.