HashTable vs Dictionary

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

//   ———————————————————————————————————————
//  Differences
//  On HashTable, we cannot apply KeyValuePair enumeration
//  On HashTable , if give the Unknown Key, it will return null whereas On Dictionary, it will throw runtime error if we try to access the Value
//            ***If you use indexers to get a value out of a HashTable, the HashTable will successfully return null for a non-existent item, whereas the Dictionary will throw an error if you try accessing a item using a indexer which does not exist in the Dictionary
//  On HashTable, Only way to access value is by passing the Key(not ViceVersa operation succeeds)
//   ———————————————————————————————————————

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Dictionary myDict = new Dictionary();
Hashtable objHashTable = new Hashtable();
public Form1()
{
InitializeComponent();
LoadDict();
MessageBox.Show(“Hash Results”);
LoadHash();
}
private void LoadDict()
{
myDict.Add(2, “Anu”);
myDict.Add(1, “Shalini”);
myDict.Add(3, “Archu”);
foreach (KeyValuePair v in myDict)
{
MessageBox.Show(string.Concat(v.Key, v.Value));
}
//for (int i = 0; i < myDict.Count; i++)
//{
//    MessageBox.Show(string.Concat(myDict[i]));
//}
}
private void LoadHash()
{
objHashTable.Add(2, “Anu”);
objHashTable.Add(1, “Shalini”);
objHashTable.Add(3, “Archu”);
objHashTable.Add(“bujji”, 4);
//foreach (KeyValuePair v in objHashTable)
//{
//    MessageBox.Show(string.Concat(v.Key,v.Value));
//}
for (int i = 0; i < objHashTable.Count; i++)
{
MessageBox.Show(string.Concat(objHashTable[i]));
}
//MessageBox.Show(objHashTable[“Shalini”].ToString());
MessageBox.Show(objHashTable[“bujji”].ToString());
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Close();
}

}
}

Advertisements

By Ref and By Value

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;

namespaceWindowsFormsApplication1
{
publicclassDepartmemt
{
publicintDeptNo { get; set; }
publicstringDepartmentName { get; set; }
}


publicclassEmployee
{
publicintEmpNo { get; set; }
publicstringEmpName { get; set; }
publicDepartmemtEmpDepartment { get; set; }
}
publicpartialclassForm1: Form
{
publicForm1()
{
InitializeComponent();
Loading();
}
privatevoidLoading()
{
//New Memory Created in Heap and pointed by empObj1 in Stack
//Lets assume that empObj1 contains value(Address)->#1
Employeeempobj1 = newEmployee()
{ EmpNo = 1, EmpName = “Shalini”, EmpDepartment = newDepartmemt()
{ DeptNo = 1, DepartmentName = “.Net”} };
//No New Memory is created in Heap, instead a memory is created in stack for storing address.
//So empobj2 also contains #1
Employeeempobj2 = empobj1;
Employeeempobj3 = empobj1;
ChangeEmpDetails(empobj1);
// Values are not effected by below statement, because the following function is not working directly on #1
MessageBox.Show(empobj1.EmpName);//Output : Shalini
AssignEmpDetails(empobj2);
// Values are effected by below statement, because the following function is working directly on #2
MessageBox.Show(empobj2.EmpName);//Output : Anu

ChangeEmpDetailsThrRef(refempobj3);
// Values are effected by below statement, because the following function is working directly on variable rather than on value ie.. #1
MessageBox.Show(empobj3.EmpName);//Output : Anu
}
privatevoidChangeEmpDetails(EmployeeobjEmp)
{
//Here we recieved the #1 as input – objEmp
//New Memory Created in Heap and pointed by emp in Stack
//Lets assume that emp contains value(Address)->#2
Employeeemp = newEmployee();
emp.EmpNo = 2;
emp.EmpName = “Anu”;
emp.EmpDepartment = newDepartmemt() { DeptNo = 2, DepartmentName = “Oracle”};
//We replaced the objEmp with #2
objEmp = emp;
}


privatevoidAssignEmpDetails(EmployeeobjEmp)
{
//Here we recieved the #1 as input in objEmp
//if we refer objEmp means..we are referring to previously created memory
objEmp.EmpNo = 2;
objEmp.EmpName = “Anu”;
objEmp.EmpDepartment = newDepartmemt() { DeptNo = 2, DepartmentName = “Oracle”};
}


privatevoidChangeEmpDetailsThrRef(refEmployeeobjEmp)
{
//Here we recieved the newly created(from outside) memory
//New Memory Created in Heap and pointed by emp in Stack
//Lets assume that emp contains value(Address)->#3
Employeeemp = newEmployee();
emp.EmpNo = 2;
emp.EmpName = “Anu”;
emp.EmpDepartment = newDepartmemt() { DeptNo = 2, DepartmentName = “Oracle”};
//We replaced the objEmp with #3
objEmp = emp;
}

}
}