very new to c# here and following tutorials on PluralSight.
i have the following files (Classes i think ?)
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Customers
{
class Program
{
static void Main(string[] args)
{
CustomerList customers = new CustomerList();
customers.AddCustomer("Apple");
customers.AddCustomer("Microsoft");
}
}
}
And CustomerList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Customers
{
public class CustomerList
{
public CustomerList()
{
customers = new List<string>();
}
public void AddCustomer(string customer)
{
customers.Add(customer);
}
private List<String> customers;
}
}
The snag i'm hitting is i just want to list out the items within the list and doing so is stumping me. I did try to use a foreach statement as follows.
foreach (string customer in customers)
{
Console.WriteLine(customer);
}
from within Program.cs but i'm unable to do so as it states
"foreach statement cannot operate on variables of type 'CustomerList' because 'CustomerList' does not contain a public instance definition for 'GetEnumerator'"
However if i keep all my code within Program.cs and use the following i am able to write out each item within list.
private List<string> customers;
static void Main(string[] args)
{
customers = new List<String>();
AddCustomers(customers);
foreach(string customer in customers) {
Console.WriteLine(customer);
}
}
private void AddCustomers(List<string> customers)
{
customers.Add("Apple");
customers.Add("Microsoft");
}
Like i say, very new, so please go easy one me.