This is the first time i am working with Lists and I don't seem to be getting it right. I have a Customer class with a list of customers as a property in the Customer class (can it be done like this?)
public class Customer
{
private List<Customer> customers = new List<Customer>();
public List<Customer> Customers
{
get { return customers; }
set { customers = value; }
}
In my program I add to this customer list like this:
Customer C = new Customer();
Customer.InputCustomer(C);
C.Customers.Add(C);
Now I need to show the customers in this list. I have added a AllCustomers function to the Customer Class like this:
public static void AllCustomers()
{
foreach (Customer customer in Customers) //Fail on "Customers"
{
Console.WriteLine("Customer ID: " + customer.ID);
Console.WriteLine("Customer Name: " + customer.FullName);
Console.WriteLine("Customer Address: " + customer.Address);
Console.WriteLine();
}
}
But I am getting this error in the foreach statement:
An object reference is required for the non-static field, method, or property 'AddCustomerList.Customer.Customers.get'
Like I said, this is the first time I am using List, maby i don't understand it right? Can anyone please help me?
question from:
https://stackoverflow.com/questions/65928628/i-have-a-problem-making-a-variables-that-can-be-used-in-other-methods-and-addi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…