代码
public class Person
{
private string m_name;
private int m_birthday;
public Person(string name, int birthday)
{
m_name = name;
m_birthday = birthday;
}
public override int GetHashCode()
{
return m_birthday * m_name.GetHashCode();
}
public override bool Equals(object obj)
{
Person person = obj as Person;
if (person != null)
return (person.GetHashCode() == GetHashCode());
else
return false;
}
}
public class Article : IComparable<Article>
{
public decimal price;
public Article(decimal price)
{
this.price = price;
}
int IComparable<Article>.CompareTo(Article art)
{
return price.CompareTo(art.price);
}
}
/// <summary>
/// Dictionary遍历
/// </summary>
public static void DictionaryDisplay()
{
Dictionary<string, string> dico = new Dictionary<string, string>();
dico.Add("carl", "23");
dico.Add("hubcarl", "24");
foreach (KeyValuePair<string, string> item in dico)
{
System.Console.WriteLine(item.Key + ":" + item.Value);
}
}
/// <summary>
/// GetHashCode
/// </summary>
public static void Dictionary()
{
//object 类提供了Equals()和GetHashCode()两个方法
//Dictionary<K,V>的算法把这两个算法用于在K类型上。
//如果打算复写一个类的Equals()方法,而这个类的实例又是用于表示算列表中的键,那就该同时复写GetHashCode()方法
//以满足如下约束:对于x和y类型的所有实例,如果x.GetHashCode()和y.GetHashCode()返回两个不同的整数,
//那么x.Equals(y)和y.Equals(x)都必须返回false.
Dictionary<Person, string> dictionary = new Dictionary<Person, string>();
Person person1 = new Person("person1", 1986);
Person person2 = new Person("person2", 1987);
dictionary.Add(person1, "carl");
dictionary.Add(person2, "anan");
Console.WriteLine(dictionary.ContainsKey(person1));
}
/// <summary>
/// 自定义类型排序
/// </summary>
public void ArraySort()
{
Article [] arr={new Article(24M),new Article (25M),new Article (23M)};
Array.Sort<Article>(arr);
}
{
private string m_name;
private int m_birthday;
public Person(string name, int birthday)
{
m_name = name;
m_birthday = birthday;
}
public override int GetHashCode()
{
return m_birthday * m_name.GetHashCode();
}
public override bool Equals(object obj)
{
Person person = obj as Person;
if (person != null)
return (person.GetHashCode() == GetHashCode());
else
return false;
}
}
public class Article : IComparable<Article>
{
public decimal price;
public Article(decimal price)
{
this.price = price;
}
int IComparable<Article>.CompareTo(Article art)
{
return price.CompareTo(art.price);
}
}
/// <summary>
/// Dictionary遍历
/// </summary>
public static void DictionaryDisplay()
{
Dictionary<string, string> dico = new Dictionary<string, string>();
dico.Add("carl", "23");
dico.Add("hubcarl", "24");
foreach (KeyValuePair<string, string> item in dico)
{
System.Console.WriteLine(item.Key + ":" + item.Value);
}
}
/// <summary>
/// GetHashCode
/// </summary>
public static void Dictionary()
{
//object 类提供了Equals()和GetHashCode()两个方法
//Dictionary<K,V>的算法把这两个算法用于在K类型上。
//如果打算复写一个类的Equals()方法,而这个类的实例又是用于表示算列表中的键,那就该同时复写GetHashCode()方法
//以满足如下约束:对于x和y类型的所有实例,如果x.GetHashCode()和y.GetHashCode()返回两个不同的整数,
//那么x.Equals(y)和y.Equals(x)都必须返回false.
Dictionary<Person, string> dictionary = new Dictionary<Person, string>();
Person person1 = new Person("person1", 1986);
Person person2 = new Person("person2", 1987);
dictionary.Add(person1, "carl");
dictionary.Add(person2, "anan");
Console.WriteLine(dictionary.ContainsKey(person1));
}
/// <summary>
/// 自定义类型排序
/// </summary>
public void ArraySort()
{
Article [] arr={new Article(24M),new Article (25M),new Article (23M)};
Array.Sort<Article>(arr);
}