现在的位置: 首页 > 综合 > 正文

Generics Quiz

2012年08月04日 ⁄ 综合 ⁄ 共 436字 ⁄ 字号 评论关闭

下面这段代码为什么无法编译?

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(1010));

            pl[
0].X = 42// <-- compiler error occurs here!

            Console.WriteLine(pl[0]);


        }


    }


}

如果你对C++很熟的话, 你再试一试用标准C++ 中的vector来代替List

这个例子其实反应了net2.0泛型的又一个让人失望的地方.

抱歉!评论已关闭.