Hashtable
Written by: maffelu , 2009-09-28 11:52:29
class Student {
//Student name, such as John, Jessica or Bill
public String Name { get; set; }
//Student id
public int Id { get; set; }
//Student school, such as Leet High, or Stanford
public String School { get; set; }
/// <summary>
/// The most basic identifiable elements of a student must
/// be set when creating a Student object
/// </summary>
/// <param name="name">The name of the student</param>
/// <param name="id">The id number of the student</param>
/// <param name="school">The name of the school where the school is currently enrolled</param>
public Student(String name, int id, String school) {
this.Name = name;
this.Id = id;
this.School = school;
}
}
public override int GetHashCode() {
return this.Id;
}
public override bool Equals(object obj) {
if (!(obj is Student))
return false;
Student p = obj as Student;
return p.Id == this.Id ;
}
using System;
namespace MorkaExample {
class Student {
//Student name, such as John, Jessica or Bill
public String Name { get; set; }
//Student id
public int Id { get; set; }
//Student school, such as Leet High, or Stanford
public String School { get; set; }
/// <summary>
/// The most basic identifiable elements of a student must
/// be set when creating a Student object
/// </summary>
/// <param name="name">The name of the student</param>
/// <param name="id">The id number of the student</param>
/// <param name="school">The name of the school where the school is currently enrolled</param>
public Student(String name, int id, String school) {
this.Name = name;
this.Id = id;
this.School = school;
}
public override bool Equals(object obj) {
if (!(obj is Student))
return false;
Student p = obj as Student;
return p.Id == this.Id ;
}
public override int GetHashCode() {
return this.Id;
}
}
}
class Program {
static void Main(string[] args) {
//Create people!
//Syntax: new Student(Name, Id)
Student s1 = new Student("John Jennings", 14, "Lund University");
Student s2 = new Student("John James", 159, "Princeton");
Student s3 = new Student("John Jennings", 14, "Harvard");
Hashtable studentHashTable = new Hashtable();
//Add the Student objects to the HashTable
//Syntax: hashtable.Add(key, value)
studentHashTable.Add(s1.GetHashCode(), s1);
studentHashTable.Add(s2.GetHashCode(), s2);
studentHashTable.Add(s3.GetHashCode(), s3); //ERROR: Item has already been added. Key in dictionary: '14' Key being added: '14'
}
}
using System;
namespace MorkaExample {
class Student {
//Student name, such as John, Jessica or Bill
public String Name { get; set; }
//Student id
public int Id { get; set; }
//Student school, such as Leet High, or Stanford
public String School { get; set; }
/// <summary>
/// The most basic identifiable elements of a student must
/// be set when creating a Student object
/// </summary>
/// <param name="name">The name of the student</param>
/// <param name="id">The id number of the student</param>
/// <param name="school">The name of the school where the school is currently enrolled</param>
public Student(String name, int id, String school) {
this.Name = name;
this.Id = id;
this.School = school;
}
public override bool Equals(object obj) {
if (!(obj is Student))
return false;
Student p = obj as Student;
return p.Id == this.Id ;
}
public override int GetHashCode() {
return this.Id;
}
}
}
using System;
using System.Collections;
namespace MorkaExample {
class Program {
static void Main(string[] args) {
//Create people!
//Syntax: new Person(Name, Id)
Student s1 = new Student("John Jennings", 14, "Lund University");
Student s2 = new Student("John James", 159, "Princeton");
Hashtable studentHashTable = new Hashtable();
//Add the Person objects to the HashTable
//Syntax: hashtable.Add(key, value)
studentHashTable.Add(s1.GetHashCode(), s1);
studentHashTable.Add(s2.GetHashCode(), s2);
//Output each object hash code
foreach (object o in studentHashTable) {
Console.WriteLine("Object hashcode: {0}", o.GetHashCode());
}
Console.WriteLine();
//Output each person
Student student1 = (Student)studentHashTable[s1.GetHashCode()];
Student student2 = (Student)studentHashTable[s2.GetHashCode()];
Console.WriteLine("{0, -15} {1, -15} {2, -15}", "Name", "Id", "School");
Console.WriteLine("{0, -15} {1, -15} {2, -15}", student1.Name, student1.Id, student1.School);
Console.WriteLine("{0, -15} {1, -15} {2, -15}", student2.Name, student2.Id, student2.School);
Console.Read();
}
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.