简体   繁体   English

调用时C#方法错误

[英]C# method error when calling it

I have a assignment for my C# course, but I cant figure it out. 我有一份C#课程的作业,但我无法弄清楚。 I want to show the cars I have, but it doesn't work. 我想向我展示我拥有的汽车,但没有用。 Can someone please help me ? 有人可以帮帮我吗 ?

Program: 程序:

using System;
using System.Collections.Generic;
using System.Text;

namespace auto
{
    class Program
    {
        static void Main(string[] args)
        {

            List<auto> autos = new List<auto>();
            auto auto1 = new auto("Lamborgini" , "Aventador" , "2004");
            display();
        }

          void display(List<auto> auto)
        {
            foreach (auto item in auto)
            {
                Console.WriteLine("Merk :" + item.merk );
                Console.WriteLine("Model :" + item.model);
                Console.WriteLine("Jaar :" + item.jaar);
                Console.ReadKey();
            }
        }

    }
}

Class: 类:

using System;
using System.Collections.Generic;
using System.Text;

namespace auto
{
    class auto
    {

        public string merk { get; set; }
        public string model { get; set; }
        public string jaar { get; set; }

        public auto(string merk , string model, string jaar)
        {
            this.merk = merk;
            this.model = model;
            this.jaar = jaar;
        }




    }
}

This is the error I get: 这是我得到的错误:

Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'auto' of 'Program.display(List)' auto C:\\Users\\nickg\\source\\repos\\auto\\auto\\Program.cs 14 Active 严重性代码说明项目文件行抑制状态错误CS7036没有给出与“ Program.display(List)”所需的形式参数“ auto”相对应的参数auto C:\\ Users \\ nickg \\ source \\ repos \\ auto \\ auto \\ Program.cs 14有效

You are going to get a compilation error. 您将得到一个编译错误。 So all you need to do is pass the parameter to the function. 因此,您要做的就是将参数传递给函数。

    static void Main(string[] args)
    {
        List<auto> autos = new List<auto>();
        autos.Add(new auto("Lamborgini" , "Aventador" , "2004"));
        display(autos);
    }

Make the display method static and pass autos as the parameter when you call it. 将显示方法设为静态,并在调用它时将autos作为参数传递。

The posted code uses auto as the namespace, class name and parameter name, so I suspect sooner or later the compiler will get confused. 发布的代码将auto用作名称空间,类名和参数名,因此我怀疑迟早编译器会感到困惑。 Consider giving them different names. 考虑给他们起不同的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM