C# HashTable的一個例子.

來源:互聯網
上載者:User
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace ConsoleApplication1 //這個小程式是關於hashtable的,自己感覺就是相當於屬性。
{
public class Employeedata //一個員工資料類
{
private string name;
private decimal salary;
private EmployeeID id;

public Employeedata()
{

}
public Employeedata(EmployeeID id,string name,decimal salary)
{
this.id = id;
this.name = name;
this.salary = salary;

}
public override string ToString()
{
StringBuilder sb = new StringBuilder(id.ToString(), 100);
sb.Append(":");
sb.Append(string.Format("{0,-20}{1:c}",name,salary));
return sb.ToString();

}
}
public class EmployeeID //員工ID 類
{
private string iD;

public string ID
{
get { return iD; }
set { iD = value; }
}
public EmployeeID(string id)
{
this.iD = id;

}
public override string ToString()
{
return iD.ToString();
}

}

public class TestHarness
{
public Hashtable employeses = new Hashtable(53);

public void Run()

{
EmployeeID id1=new EmployeeID("2050");
EmployeeID id2=new EmployeeID("24210");
Employeedata myEmployee01 = new Employeedata(id1, "張三", 12613213.44m);
Employeedata myEmployee02= new Employeedata(id2, "李四", 145672228883.44m);
employeses.Add(id1, myEmployee01);
employeses.Add(id2, myEmployee02);

while (true)
{
try
{
Console.WriteLine("Enter employee ID(格式:0-9,'X'退出!):");
string userInput=Console.ReadLine();
userInput=userInput.ToUpper();
if(userInput=="X")
return;
EmployeeID id=new EmployeeID(userInput);
DisplayData(id);

}
catch (Exception e)
{
Console.WriteLine("Exception occurred.Did you use the correct format for the employee ID?");
Console.WriteLine(e.Message);
Console.WriteLine();

}
Console.WriteLine();
}

}
public void DisplayData(EmployeeID id)
{
//Console.WriteLine(id.ToString());

object emObj = employeses[id];
if (emObj != null)
{
Employeedata employeeTemp = (Employeedata)emObj;
Console.WriteLine("Employee:" + employeeTemp.ToString());
}
else
{
Console.WriteLine("Employee not found:ID = "+id.ToString());
}
}

}
class Program
{
static void Main(string[] args)
{

TestHarness test = new TestHarness(); //在這裡本來是打算用foreach的,但是不知道雜的用不了,要用foreach 還是用ArrayList吧。
test.Run();

}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.