在下面的代码中,我们的字典的类型是<int, Customer>
,但是我怎么知道客户的类型是什么?由于我们是Customer cust1 = new Customer(1,“Cust 1”),因此Customer看起来像是一个字符串。 .... im confused ...关于C#通用集合的问题
public class Customer
{
public Customer(int id, string name)
{
ID = id;
Name = name;
}
private int m_id;
public int ID
{
get { return m_id; }
set { m_id = value; }
}
private string m_name;
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
Customer cust1 = new Customer(1, "Cust 1");
Customer cust2 = new Customer(2, "Cust 2");
Customer cust3 = new Customer(3, "Cust 3");
customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust2);
customers.Add(cust3.ID, cust3);
foreach (KeyValuePair<int, Customer> custKeyVal in customers)
{
Console.WriteLine(
"Customer ID: {0}, Name: {1}",
custKeyVal.Key,
custKeyVal.Value.Name);
}
如果您有任何其他问题,请随时提问......看起来您可能对面向对象或某种性质有点新的东西。 – Polaris878 2009-08-02 16:15:07