C# 从对象列表中选择随机对象

C# 从对象列表中选择随机对象,c#,random,C#,Random,我试图从列表中随机选择一个客户项目。我不知道该怎么做才能真正打印出列表中的信息 我把它作为我的客户类 namespace PizzaParlor { class Customer { private string name; private int flavor; private int price; private int quality; private int speed; pr

我试图从列表中随机选择一个客户项目。我不知道该怎么做才能真正打印出列表中的信息

我把它作为我的客户类

namespace PizzaParlor
{
    class Customer
    {
        private string name;
        private int flavor;
        private int price;
        private int quality;
        private int speed;
        private int accessibility;
        private int brand;
        private int convenience;
        private int variety;

        public Customer(string name, int flavor, int price, int quality, int speed, int accessibility, int brand, int convenience, int variety)
        {
            this.name = name;
            this.flavor = flavor;
            this.price = price;
            this.quality = quality;
            this.speed = speed;
            this.accessibility = accessibility;
            this.brand = brand;
            this.convenience = convenience;
            this.variety = variety;
        }

        // Name, Speed, Quality, Variety, Convenience, Accessibility, price, brand, flavor
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public int Quality
        {
            get { return quality; }
            set { quality = value; }
        }

        public int Variety
        {
            get { return variety; }
            set { variety = value; }
        }

        public int Convenience
        {
            get { return convenience; }
            set { convenience = value; }
        }

        public int Accessibility
        {
            get { return accessibility; }
            set { accessibility = value; }
        }

        public int Price
        {
            get { return price; }
            set { price = value; }
        }

        public int Brand
        {
            get { return brand; }
            set { brand = value; }
        }

        public int Flavor
        {
            get { return flavor; }
            set { flavor = value; }
        }
    }
}
这是我的主要类,我设置它来与customer类一起工作:

namespace PizzaParlor 
{
    class Program
    {
        static void Main(string[] args)
        {
            var random = new Random();

            List<Customer> CustomerList = new List<Customer>();

            CustomerList.Add(new Customer("bill", 20,15,10,5,10,20,5,15));
            CustomerList.Add(new Customer("kevin", 15, 10, 5, 20, 15, 15, 0, 20));
            CustomerList.Add(new Customer("clair", 8,25,2,25,5,15,0,20));
            CustomerList.Add(new Customer("jim", 15,20,10,15,0,40,0,0));
            CustomerList.Add(new Customer("rachel", 20,15,10,5,10,30,0,10));
            CustomerList.Add(new Customer("jeff", 30,20,5,5,10,10,0,20));
            CustomerList.Add(new Customer("Mike", 21,23,0,10,14,16,0,16));
            CustomerList.Add(new Customer("john", 25,15,10,10,10,5,5,20));

            int index = random.Next(CustomerList.Count);

            Console.WriteLine(CustomerList[index]);
        }
    }
}
我知道random.NextCustomerList.Count将从列表中随机选择一个,但我不知道它为什么返回此输出:


这是因为当您尝试打印对象(例如Customer)时,将执行ToString的默认实现。这将生成您看到的输出

有两种方法可以修复它

打印您感兴趣的显式字段 重写ToString实现 在你的主要方法中

int index = random.Next(CustomerList.Count);
var customer = CustomerList[index];
Console.WriteLine(customer);

您可以使用反射来实现这一点

 foreach (var prop in typeof(Customer).GetProperties())
            {
                Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(CustomerList[index]));
            }

因为你只是在打印对象本身。CustomerList[index]。例如,Name将打印名称。您实际上正在打印对象本身。请尝试控制台。WriteLineCustomerList[index]。Name;非常感谢你们!是否有一种方法可以打印出名称以及它们拥有的所有值?
int index = random.Next(CustomerList.Count);
var customer = CustomerList[index];
Console.WriteLine(customer);
 foreach (var prop in typeof(Customer).GetProperties())
            {
                Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(CustomerList[index]));
            }