将多个项添加到列表中

wil*_*lla 12 c#

static class Program
{
    static void Main(string carMake, string carModel, string carColour, string bikeModel, string bikeMake, string bikeColour, string truckMake, string truckModel, string truckColour)
    {
        car Mynewcar = new car();
        motorbike Mynewbike = new motorbike();
        truck Mynewtruck = new truck();

        int choice = 0;
        while (choice != 5)
        {

            Console.WriteLine("MENU");
            Console.WriteLine("What service do you need");
            Console.WriteLine("1. Car");
            Console.WriteLine("2. Motorbike");
            Console.WriteLine("3. Truck");
            Console.WriteLine("4. Search");
            Console.WriteLine("5. Exit");

            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.WriteLine("What is the car make?");
                    Mynewcar.make = Console.ReadLine().ToLower();
                    carMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("What is the car model?");
                    Mynewcar.model = Console.ReadLine().ToLower();
                    carModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("What is the car Colour?");
                    Mynewcar.colour = Console.ReadLine().ToLower();
                    carColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 2:
                    Console.WriteLine("what is the motorbike make");
                    Mynewbike.make = Console.ReadLine().ToLower();
                    bikeMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the motorbike model");
                    Mynewbike.model = Console.ReadLine().ToLower();
                    bikeModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the motorbike colour");
                    Mynewbike.colour = Console.ReadLine().ToLower();
                    bikeColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 3:
                    Console.WriteLine("what is the trucks make");
                    Mynewtruck.make = Console.ReadLine().ToLower();
                    truckMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the trucks model");
                    Mynewtruck.model = Console.ReadLine().ToLower();
                    truckModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the trucks colour");
                    Mynewtruck.colour = Console.ReadLine().ToLower();
                    truckColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 4:
                    string searchchoice = "";
                    Console.WriteLine("select Car, Motobike or truck to search?");
                    searchchoice = Console.ReadLine().ToLower();
                    if (searchchoice.Equals("car"))
                    {
                        Console.WriteLine("Car in inventory: {0} - {1} - {2}", carMake, carModel, carColour);
                    }
                    else if (searchchoice.Equals("motorbike"))
                    {
                        Console.WriteLine("Motorbike in inventory: {0} - {1} - {2}", bikeMake, bikeModel, bikeColour);
                    }
                    else
                    {
                        Console.WriteLine("Trucks in inventory: {0} - {1} - {2}", truckMake, truckModel, truckColour);
                    }
                    Console.ReadLine();
                    break;
                case 5:
                    break;
                default:
                    Console.WriteLine("Sorry, invalid selection");
                    break;
            }
        }
    }

    class car
    {
        public string make { get; set; }
        public string model { get; set; }
        public string colour { get; set; }

        public List<String> carList(car Mynewcar)
        {
            List<String> caradd = new List<String>();
            caradd.Add(Mynewcar.make);
            string carMake = Mynewcar.make;

            caradd.Add(Mynewcar.model);
            string carModel = Mynewcar.model;

            caradd.Add(Mynewcar.colour);
            string carColour = Mynewcar.model;


            return caradd;
        }

    }
    class motorbike : car
    {
        public List<String> bikeList(motorbike Mynewbike)
        {
            List<String> bikeadd = new List<String>();
            bikeadd.Add(Mynewbike.model);
            string bikeModel = Mynewbike.model;

            bikeadd.Add(Mynewbike.make);
            string bikeMake = Mynewbike.make;

            bikeadd.Add(Mynewbike.colour);
            string bikeColour = Mynewbike.colour;

            return bikeadd;
        }
    }
    class truck : car
    {
        public List<String> truckList(truck Mynewtruck)
        {
            List<String> truckadd = new List<String>();
            truckadd.Add(Mynewtruck.make);
            string truckMake = Mynewtruck.make;

            truckadd.Add(Mynewtruck.model);
            string truckModel = Mynewtruck.model;

            truckadd.Add(Mynewtruck.colour);
            string truckColour = Mynewtruck.colour;

            return truckadd;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是想知道是否有人可以查看我的代码,看看我可以改进哪些方面.

此外,我正在努力为汽车,自行车和卡车的列表中添加多个项目.你会怎么做,这样你可以添加属性的多个实例并将它们列出到控制台?

Mal*_*ick 20

感谢AddRange:

例:

public class Person
{ 
    private string Name;
    private string FirstName;

    public Person(string name, string firstname) => (Name, FirstName) = (name, firstname);
}
Run Code Online (Sandbox Code Playgroud)

要添加多个PersonList<>:

List<Person> listofPersons = new List<Person>();
listofPersons.AddRange(new List<Person>
{
    new Person("John1", "Doe" ),
    new Person("John2", "Doe" ),
    new Person("John3", "Doe" ),
 });
Run Code Online (Sandbox Code Playgroud)

  • 解释清楚且完整的示例。这应该是公认的答案。 (2认同)

Jer*_*vel 14

代码检查:

这是offtopic,但是CodeReview的人们非常乐意为您提供帮助.

我强烈建议你这样做,你的代码中有几件需要注意的事情.同样地,我建议你开始阅读教程,因为没有充分的理由不这样做.

列表:

正如你自己说的:你需要一个项目清单.它现在的方式只存储对一个项目的引用.幸运的是,有一组相关的对象:a List.

列表非常简单易用,但无论如何都要查看相关文档.

在列表中保留多个自行车的一个非常简单的示例:

List<Motorbike> bikes = new List<Motorbike>();

bikes.add(new Bike { make = "Honda", color = "brown" });
bikes.add(new Bike { make = "Vroom", color = "red" });
Run Code Online (Sandbox Code Playgroud)

要迭代列表,您可以使用以下foreach语句:

foreach(var bike in bikes) {
     Console.WriteLine(bike.make);
}
Run Code Online (Sandbox Code Playgroud)


jor*_*mer 8

在使用扩展方法、泛型和params关键字的现代 C# 中,您可以在 List 类上创建泛型方法,该方法允许您将无限数量的项目添加到包含任何类型的列表中:

static class ListExtensions 
{
  static public void Add<T>(this List<T> list, params T[] additions)
  {
    foreach(T addition in additions)
    {
      list.Add(addition)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用字符串列表和整数列表的示例用法:

var stringList = new List<string>();
stringList.Add("Hello", "World", "!");

var intList = new List<int>();
intList.Add(1, 2, 3, 4, 5);
Run Code Online (Sandbox Code Playgroud)


小智 6

另一种有用的方法是使用Concat
更多信息请参见官方文档

List<string> first = new List<string> { "One", "Two", "Three" };
List<string> second = new List<string>() { "Four", "Five" };
first.Concat(second);
Run Code Online (Sandbox Code Playgroud)

输出将是。

One
Two
Three
Four
Five
Run Code Online (Sandbox Code Playgroud)

还有另一个类似的答案