Implement GetHashCode method and store it in a hashtable : Hashtable : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Hashtable »

 

Implement GetHashCode method and store it in a hashtable









    


using System;
using System.Collections;

public class GoodCompare {
  public static void Main() {
    Name president = new Name ("A", "B");
    Name first = new Name ("C", "D");
    
    Hashtable m = new Hashtable();
    m.Add(president, "first");
    Console.WriteLine(m.Contains(first));
    Console.WriteLine(m[first]); 
  }
}



public class Name {
  protected String first;
  protected char initial;
  protected String last;
          
  public Name(String f, String l) {
    first = f; 
    last = l; 
  }
  public Name(String f, char i, String l) : this(f,l) {
    initial = i;  
  } 
  public override String ToString() {
    if (initial == '\u0000')
       return first + " " + last;
    else  
       return first + " " + initial + " " + last;
  }
  public override bool Equals(Object o) {
    if (!(o is Name))
       return false;
    Name name = (Name)o;
    return first == name.first && initial == name.initial
             && last == name.last;
  }
  public override int GetHashCode() {
    return first.GetHashCode() + (int)initial 
                             + last.GetHashCode();
  }

}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Hashtable