如何获取在Main中的客户中分配的客户类的每个元素(名字,姓氏)。我无法在主要内容customers.firstname
。
public static class CustomerProvider
{
public static List<Customer> GetRandomCustomers()
{
var result = new List<Customer>();
var customer1 = new Customer();
customer1.FirstName = "Tony";
customer1.LastName = "Romo";
customer1.DateOfBirth = Convert.ToDateTime("03 / 10 / 88");
result.Add(customer1);
return result;
}
static void Main(string[] args)
{
var customers= CustomerProvider.GetRandomCustomers();
foreach (var c in customers)
{
Console.WriteLine(customers);
}
Console.ReadLine();
Console.ReadLine();
}
答案 0 :(得分:1)
了解foreach
正在做什么非常重要。
customers
是Customer
课程的集合,c
每次循环都是Customer
。{/ p>
所以你会这样写:
foreach (var c in customers)
{
Console.WriteLine(c.FirstName);
Console.WriteLine(c.LastName);
}
答案 1 :(得分:0)
应该是这样的:
foreach (var c in customers)
{
Console.WriteLine(c.FirstName);
Console.WriteLine(c.LastName);
}
答案 2 :(得分:0)
在你的foreach中,你写出了错误的变量:
foreach (var c in customers)
{
Console.WriteLine(c.FirstName);
}
答案 3 :(得分:0)
简单
foreach (var c in customers)
{
Console.WriteLine(c.FirstName );
Console.WriteLine(c.LastName );
Console.WriteLine(c.DateOfBirth );
}
答案 4 :(得分:0)
您可以通过c
foreach变量执行此操作。
foreach (var c in customers)
{
Console.WriteLine(c.FirstName);
}