关于C#Generic Collection的问题
发布时间:2020-12-16 10:38:35 所属栏目:百科 来源:网络整理
导读:在下面的代码中,我们的Dictionary的类型是 int,Customer,但我怎么知道Customer的类型是什么?看来客户是一个字符串,因为我们是客户cust1 =新客户(1,“客户1”); …. 我糊涂了… public class Customer { public Customer(int id,string name) { ID = id; Nam
在下面的代码中,我们的Dictionary的类型是< int,Customer>,但我怎么知道Customer的类型是什么?看来客户是一个字符串,因为我们是客户cust1 =新客户(1,“客户1”); …. 我糊涂了…
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); } 解决方法
Customer对象的类型为Customer,即使它包含int和字符串.当您致电客户cust1 =新客户(1,“客户1”)时;这真的是说“让我成为Customer类型的对象,它包含整数1和字符串Cust 1”
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |