Коллекция OrderedDictionary и сравнение значений - C#
Формулировка задачи:
Задание: Создайте коллекцию типа OrderedDictionary и реализуйте в ней возможность сравнения значений
Не могу разобраться, что я должен сравнивать в методе Equals. Да и прилепить свой экземпляр OrderedDictionaryComparer к коллекции не выходит. Помогите разобраться.
Мой быдлокод :
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Задание 4
//Создайте коллекцию типа OrderedDictionary и реализуйте в ней возможность сравнения значений.
namespace ConsoleApplication40
{
class Program
{
static void Main(string[] args)
{
OrderedDictionaryComparer myComparer = new OrderedDictionaryComparer();
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1","один");
dict.Add("2", "два");
dict.Add("3", "один1");
Console.ReadLine();
}
}
class OrderedDictionaryComparer : IEqualityComparer<KeyValuePair<string, string>>
{
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
{
if (x.Value == y.Value)
{
return true;
}
return false;
}
public int GetHashCode(KeyValuePair<string, string> obj)
{
return obj.Value.GetHashCode();
}
}
}Решение задачи: «Коллекция OrderedDictionary и сравнение значений»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
OrderedDictionaryComparer Comparer = new OrderedDictionaryComparer();
OrderedDictionary dict1 = new OrderedDictionary(Comparer);
dict1.Add("1", "один");
dict1.Add("2", "два");
dict1.Add("3", "три");
OrderedDictionary dict2 = new OrderedDictionary();
dict2.Add("1", "один");
foreach (DictionaryEntry item1 in dict1)
{
foreach (DictionaryEntry item2 in dict2)
{
Console.WriteLine(item1.Equals(item2).ToString());
}
}
Console.ReadLine();
}
}
class OrderedDictionaryComparer: IEqualityComparer
{
public bool Equals(DictionaryEntry x, DictionaryEntry y)
{
if (x.Value != null && y.Value != null)
{
if (x.Value == y.Value)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool Equals(object x, object y)
{
if (x != null && y != null)
{
if (x == y)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public int GetHashCode(DictionaryEntry obj)
{
return obj.Value.GetHashCode();
}
public int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}