Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表需要一个类型参数_C#_List_Oop_Object Oriented Analysis - Fatal编程技术网

C# 列表需要一个类型参数

C# 列表需要一个类型参数,c#,list,oop,object-oriented-analysis,C#,List,Oop,Object Oriented Analysis,编译代码时出现三个错误。 1.使用泛型类型列表需要1个参数。 2.使用泛型类型列表需要1个参数。 3.foreach语句无法对类型的变量进行操作,因为列表不包含“GetEnumerator”的公共定义 下面是多态性示例的程序 namespace PolymorExample { abstract class Shape { public abstract void area(); } class Rectangle : Shape {

编译代码时出现三个错误。 1.使用泛型类型列表需要1个参数。 2.使用泛型类型列表需要1个参数。 3.foreach语句无法对类型的变量进行操作,因为列表不包含“GetEnumerator”的公共定义

下面是多态性示例的程序

namespace PolymorExample
{
    abstract class Shape
    {
        public abstract void area();
    }

    class Rectangle : Shape
    {
        private double length;
        private double width;

        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        public override void area()
        {
            Console.WriteLine("Rectangel Area: {0}", length * width);
        }
    }

    class Triangle : Shape
    {
        private double baseline;
        private double height;

        public Triangle(double baseline, double height)
        {
            this.baseline = baseline;
            this.height = height;
        }

        public override void area()
        {
            Console.WriteLine("Triangel Area: {0}", baseline * height / 2.0);
        }
    }

    class Circle : Shape
    {
        const double PI = 3.14;
        private double radius;

        public Circle(double radius)
        {
            this.radius = radius;
        }

        public override void area()
        {
            Console.WriteLine("Circle Area: {0}", radius * radius * PI);
        }
    }

    public class TestShape
    {
        static void Main()
        {
            List shapes = new List();
            Shape shape1 = new Rectangle(10, 10);
            shapes.Add(shape1);
            shapes.Add(new Circle(10));
            shapes.Add(new Triangle(10, 10));
            shapes.Add(new Circle(20));

            foreach (Shape s in shapes)
            {
                s.area();
            }

            Console.Read();
        }
    }
}
列表形状=新列表();

在列表声明中需要形状类型,以便它知道什么是列表。如果您查看列表的文档,您会注意到列表是一种类型(因此是
),泛型类型需要一个参数(或更多)来指定它将使用/包含的对象的类型。您必须指定某种类型,即使它只是
对象

在您的情况下,您有一个
形状
对象列表,因此可以修改初始化代码(并使用集合初始值设定项语法简化),以指定该类型:

var shapes = new List<Shape>
{
    new Rectangle(10, 10),
    new Circle(10),
    new Triangle(10, 10),
    new Circle(20)
};
var shapes=新列表
{
新矩形(10,10),
新圆圈(10),
新三角形(10,10),
新圈子(20)
};
Try
List
。据我所知,.NET Framework中没有非通用的
List
类型。您必须指定一个类型参数:
List
var shapes = new List<Shape>
{
    new Rectangle(10, 10),
    new Circle(10),
    new Triangle(10, 10),
    new Circle(20)
};