Hashtable的用法

发表于:2007-07-01来源:作者:点击数: 标签:
这个例子演示了Hashtable的用法,例子比较简单,主要是下面两个类的一些方法的override: public class EmployeeID { private readonly char prefix; private readonly int number; public EmployeeID( string id ) { prefix = (id.ToUpper())[0]; number =


    这个例子演示了Hashtable的用法,例子比较简单,主要是下面两个类的一些方法的override:

public class EmployeeID
 {
  private readonly char prefix;
  private readonly int  number;

  public EmployeeID( string id )
  {
   prefix = (id.ToUpper())[0];
   number = int.Parse ( id.Substring(1,3) );
  }

  public override string ToString()
  {
   return prefix.ToString () + string.Format( "{0,3:000}",number );
  }
  
  public override int GetHashCode()
  {
   return this.ToString().GetHashCode();
  }
  
  public override bool Equals(object obj)
  {
   EmployeeID rhs = obj as EmployeeID ;
   if( rhs == null )
    return false;
   if( prefix == rhs.prefix && number == rhs.number )
    return true;
   return false;
  }

 }

public class EmployeeData
 {
  private string name;
  private decimal salary;
  private EmployeeID id;

  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}", name));
   sb.Append (" ");
   sb.Append (string.Format ( "{0:C}",salary));
   return sb.ToString ();
  }

 }

    具体的请参考源代码。


原文转自:http://www.ltesting.net