运行错误求更正
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Example_HashTest
{
//创建电话本类,属性有姓名和电话
public class TelNote
{
private string name;//姓名
public string Name
{
get { return name; }
set { name = value; }
}
private string number;//电话
public string Number
{
get { return number; }
set { number = value; }
}
}
public class TeleNoteEdit
{
public Hashtable httels = new Hashtable();//创建哈希表
//添加电话信息
public void Add(TelNote tel)
{//如果电话本为空则存储电话
if (httels.Count == 0)
{
httels.Add(tel.Name, tel.Number );
}
else
{//如果电话本不为空,检查是否存在该姓名
if (httels.Contains(tel.Name))
{
httels[tel.Name] = tel.Number;//如果存在则修改其电话
}
else
{
httels.Add(tel.Name, tel.Number );//如果不存在,则新建电话信息
}
}
}
//根据姓名返回电话信息
public TelNote GetByName(string name)
{
foreach (object tel in httels.Values)
{
TelNote mytel = (TelNote)tel;
if (name == mytel.Name)
{
return mytel;
}
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
TelNote mytell = new TelNote();
mytell.Name = "范跑跑";
mytell.Number = "81161234";
//将信息添加到哈希表中
TeleNoteEdit teledit = new TeleNoteEdit();
teledit.Add(mytell);
Console.WriteLine("哈希表中元素的个数{0}", teledit.httels.Count);
foreach (DictionaryEntry myde in teledit.httels)
{
Console.WriteLine("\t{0}\t{1}", myde.Key, myde.Value);
}
//查询范跑跑的电话
TelNote mytel = teledit.GetByName("范跑跑");
Console.WriteLine("{0}的电话{1}", mytel.Name, mytel.Number);
Console.ReadKey();
}
}
}
--------------------编程问答-------------------- 如何设计小型应用程序-WPF下实现通讯录 --------------------编程问答-------------------- 如果你用这个程序来学习HashTable,只能说明你根本就不理解HashTable。
姓名无论如何也不能作为Key。
补充:.NET技术 , C#