泛型应用(1)
一直以来对泛型的 理解一直很 浮浅,需要多 学习理解
Customer.cs 代码:
public class Customer
{
private string customerName = "";
public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}
public Customer(string customerName)
{
this.customerName = customerName;
}
}
是对员工的定义
CustomerList.cs代码
public class CustomerList : CollectionBase
{
public CustomerList()
{
}
public Customer this[int index]
{
get
{
return (Customer)List[index];
}
set
{
List[index] = value;
}
}
public int Add(Customer value)
{
return List.Add(value);
}
}
调用:
IList<Customer> customers = new List<Customer>();

//List <Customer> customers=new List<Customer>() ;

customers.Add(new Customer("jhtchina_suzsoft"));
customers.Add(new Customer("test1_microsoft"));
string str_temp = string.Empty;
foreach (Customer c in customers)
{
str_temp +="|||||"+ c.CustomerName;
}
MessageBox.Show(str_temp);
或者
public class List<T> : CollectionBase
{
public List()
{
}
public T this[int index]
{
get
{
return (T)List[index];
}
set
{
List[index] = value;
}
}
public int Add(T value)
{
return List.Add(value);
}
}
调用:
List <Customer> customers=new List<Customer>() ;

customers.Add(new Customer("jhtchina_suzsoft"));
customers.Add(new Customer("test1_microsoft"));
string str_temp = string.Empty;
foreach (Customer c in customers)
{
str_temp +="|||||"+ c.CustomerName;
}
MessageBox.Show(str_temp);
这个只是很初级的
需要继续学习
关于HashTable的一些应用
Hashtable _simpleTable1= new Hashtable();
Customer cust=new Customer("jhtchina");
cust.CustomerName ="jhtchina_CustomerName";
_simpleTable1.Add("jhtchina_Key",cust);





Customer ht = (Customer)_simpleTable1["jhtchina_Key"];

//MessageBox.Show(ht.CustomerName);





//convert HashTable to Dictionary
Dictionary<string, Customer> tmpDic = new Dictionary<string, Customer>();
string aKey = "jhtchina_Key";
tmpDic[aKey] = (Customer)_simpleTable1[aKey];
MessageBox.Show(tmpDic[aKey].CustomerName);




//HashTable
Hashtable _simpleTable = new Hashtable();

_simpleTable.Add("Key1", "Value1");
_simpleTable.Add("Key2", "Value2");




int i_count = _simpleTable.Count;

IDictionaryEnumerator _enumerator = _simpleTable.GetEnumerator();

string _string="";
while (_enumerator.MoveNext())
{
_string += _enumerator.Key + " ";
_string += _enumerator.Value + "\n";
}

MessageBox.Show(_string);

//if (_simpleTable.ContainsKey("Key1"))
//{
// Console.WriteLine("Key1 is present");
//}

//if (_simpleTable.ContainsValue("Value1"))
//{

// Console.WriteLine("Value1 is present");

//}





_simpleTable.Clear();
Customer.cs 代码:
public class Customer
{
private string customerName = "";
public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}
public Customer(string customerName)
{
this.customerName = customerName;
}
}CustomerList.cs代码
public class CustomerList : CollectionBase
{
public CustomerList()
{
}
public Customer this[int index]
{
get
{
return (Customer)List[index];
}
set
{
List[index] = value;
}
}
public int Add(Customer value)
{
return List.Add(value);
}
}
调用:
IList<Customer> customers = new List<Customer>();
//List <Customer> customers=new List<Customer>() ;
customers.Add(new Customer("jhtchina_suzsoft"));
customers.Add(new Customer("test1_microsoft"));
string str_temp = string.Empty;
foreach (Customer c in customers)
{
str_temp +="|||||"+ c.CustomerName;
}
MessageBox.Show(str_temp);或者
public class List<T> : CollectionBase
{
public List()
{
}
public T this[int index]
{
get
{
return (T)List[index];
}
set
{
List[index] = value;
}
}
public int Add(T value)
{
return List.Add(value);
}
}调用:
List <Customer> customers=new List<Customer>() ;
customers.Add(new Customer("jhtchina_suzsoft"));
customers.Add(new Customer("test1_microsoft"));
string str_temp = string.Empty;
foreach (Customer c in customers)
{
str_temp +="|||||"+ c.CustomerName;
}
MessageBox.Show(str_temp);这个只是很初级的
需要继续学习
关于HashTable的一些应用
Hashtable _simpleTable1= new Hashtable();
Customer cust=new Customer("jhtchina");
cust.CustomerName ="jhtchina_CustomerName";
_simpleTable1.Add("jhtchina_Key",cust);




Customer ht = (Customer)_simpleTable1["jhtchina_Key"];
//MessageBox.Show(ht.CustomerName);




//convert HashTable to Dictionary
Dictionary<string, Customer> tmpDic = new Dictionary<string, Customer>();
string aKey = "jhtchina_Key";
tmpDic[aKey] = (Customer)_simpleTable1[aKey];
MessageBox.Show(tmpDic[aKey].CustomerName);



//HashTable
Hashtable _simpleTable = new Hashtable();
_simpleTable.Add("Key1", "Value1");
_simpleTable.Add("Key2", "Value2");



int i_count = _simpleTable.Count;
IDictionaryEnumerator _enumerator = _simpleTable.GetEnumerator();
string _string="";
while (_enumerator.MoveNext())
{
_string += _enumerator.Key + " ";
_string += _enumerator.Value + "\n";
}
MessageBox.Show(_string);
//if (_simpleTable.ContainsKey("Key1"))
//{
// Console.WriteLine("Key1 is present");
//}
//if (_simpleTable.ContainsValue("Value1"))
//{
// Console.WriteLine("Value1 is present");
//}




_simpleTable.Clear();

浙公网安备 33010602011771号