c#简单的通讯录

(一)功能介绍 【c#简单的通讯录】1)实现添加联系人
2)实现查找联系人
3)实现修改联系人
4)实现删除联系人
5)实现退出功能
(二)功能实现 代码:

using System; using System.Collections; namespace MyClass { class Program { static void Main(string[] args) { try { Console.WriteLine(""); while (true) { Console.WriteLine("通信录管理"); Console.WriteLine("\t1.新建联系人"); Console.WriteLine("\t2.查找联系人"); Console.WriteLine("\t3.修改联系人"); Console.WriteLine("\t4.删除联系人"); Console.WriteLine("\t5.退出"); Console.WriteLine(); string choose = Console.ReadLine().Trim(); switch (choose) { case "1": AddItem(); break; case "2": SearchItem(); break; case "3": EditItem(); break; case "4": RemoveItem(); break; case "5": return; default: Console.WriteLine("选择错误!"); break; }} } catch (Exception e) { Console.WriteLine(e.Message); } } private static Hashtable hashtable = new Hashtable(); public static void AddItem() { try { Console.Write("姓名:"); string name = Console.ReadLine().Trim(); if (!hashtable.ContainsKey(name)) { Console.Write("电话号码:"); string number = Console.ReadLine().Trim(); hashtable[name] = number; } else { Console.WriteLine("{0}已经存在!",name); } } catch(Exception e) { throw e; } }public static void EditItem() { try { Console.Write("姓名:"); string name = Console.ReadLine().Trim(); if (hashtable.ContainsKey(name)) { Console.Write("电话号码:"); string number = Console.ReadLine().Trim(); hashtable[name] = number; } else { Console.Write("{0}不存在!", name); } }catch(Exception e) { throw e; } }public static void SearchItem() { try { Console.Write("\n姓名:"); string name = Console.ReadLine().Trim(); if (hashtable.ContainsKey(name)) { Console.WriteLine("{0}电话号码是{1}", name, (string)hashtable[name]); } else { Console.WriteLine("{0}不存在!", name); } } catch (Exception e) { throw e; } }public static void RemoveItem() { try { Console.Write("\n姓名:"); string name = Console.ReadLine().Trim(); if (hashtable.ContainsKey(name)) { hashtable.Remove(name); Console.WriteLine("{0}删除成功", name); } else { Console.WriteLine("{0}不存在!",name); } } catch(Exception e) { throw e; } } } }

注:
异常处理单独抽取出一个方法,减少冗余代码

    推荐阅读