简体   繁体   English

在c#中从列表中显示类的数据

[英]Display data of class from list in c#

I was trying to display data of class from list in C#.我试图从 C# 中的列表中显示类的数据。

Here is my class definition:这是我的类定义:

    class Vehicle
    {
        public string brand;
        public string model;
    }

    class Car : Vehicle
    {
        double trunk_capacity;            
        int doors_number;
        int vmax;
    }

Here is my code in which i'm adding car element:这是我添加汽车元素的代码:

    public static void AddCar(List<object> avaliable_cars)
    {
        int x = 0;

        Console.WriteLine("Adding new car:");
        Console.WriteLine("1 - Car");
        Console.WriteLine("2 - Sport Car");
        Console.WriteLine("3 - Truck");

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

        switch (x)
        {
            case 1: 
            {
                Console.WriteLine("Adding new Car.");

                avaliable_cars.Add(new Car());
                Console.WriteLine("Brand");
                string brand_tmp = Console.ReadLine();
                Console.WriteLine("Model");
                string model_tmp = Console.ReadLine();
                avaliable_cars.Add(new Car()
                {
                    brand = brand_tmp,
                    model = model_tmp
                });

            } break;
        }

   }

I don't have idea how can i display data from this list.我不知道如何显示此列表中的数据。

I was trying to do this我试图这样做

foreach (object car in avaliable_cars)
{
  Console.WriteLine(car);
}

I doesn't works.我不工作。 Also i want to access one element at one time - for example i want to display brand of first car on the list.此外,我想一次访问一个元素 - 例如,我想在列表中显示第一辆车的品牌。 How can i do this?我怎样才能做到这一点?

PS Im beginner in C#, so im sorry for any stupid elements in my code. PS 我是 C# 的初学者,所以我很抱歉我的代码中有任何愚蠢的元素。

One clean way of doing this is to override ToString()一种干净的方法是覆盖 ToString()

class Vehicle
{
    public string brand;
    public string model;
    public override ToString()
    {
        return "Brand: " + brand + ", Model: " + model;
    }
}

class Car : Vehicle
{
    double trunk_capacity;            
    int doors_number;
    int vmax;
    public override ToString()
    {
        return base.ToString() +
           "trunk_capacity: " + trunk_capacity; // etc.
    }
}

Then you can simply use然后你可以简单地使用

Console.WriteLine(car);

You need cast the object car to the type Car or Vehicle and then print.您需要将对象 car 转换为 Car 或 Vehicle 类型,然后打印。

foreach (object car in avaliable_cars)
{
  Console.WriteLine(((Vehicle)car).brand);
}

A nice way would be using generics.一个很好的方法是使用泛型。 You could specialize your method AddCar like this:您可以像这样专门化您的方法 AddCar:

static void AddCar<T>(List<T> avaliable_cars) 
       where T : Car ,new()
    {
        int x = 0;

        Console.WriteLine("Adding new car:");
        Console.WriteLine("1 - Car");
        Console.WriteLine("2 - Sport Car");
        Console.WriteLine("3 - Truck");

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

        switch (x)
        {
            case 1: 
            {
                Console.WriteLine("Adding new Car.");

                avaliable_cars.Add(new T());
                Console.WriteLine("Brand");
                string brand_tmp = Console.ReadLine();
                Console.WriteLine("Model");
                string model_tmp = Console.ReadLine();
                avaliable_cars.Add(new T()
                {
                    brand = brand_tmp,
                    model = model_tmp
                });

            } break;
        }

   }

And to print your data you can simply use T instead of object:要打印您的数据,您可以简单地使用 T 而不是 object:

foreach (T car in avaliable_cars)
{
    Console.WriteLine(car);
}

Hope it helps.希望能帮助到你。

We can simply use String.Join or foreach.我们可以简单地使用 String.Join 或 foreach。

       List<string> numbers = new List<string>
                    { "One", "Two", "Three","Four","Five"};

        Console.WriteLine(String.Join(", ", numbers));


List<string> strings = new List<string> { "a", "b", "c" };

strings.ForEach(Console.WriteLine);

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

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