Compare Data Sources - CSharp Collection

CSharp examples for Collection:IEqualityComparer

Description

Compare Data Sources

Demo Code


using System;/*w w w.j av  a  2  s . com*/
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MainClass
    {
        static void Main(string[] args)
        {
            string[] ds1 = { "Oracle", "PLSQL", "pear" };

            string[] ds2 = { "Oracle", "PLSQL", "pear" };

            string[] ds3 = { "pear", "PLSQL", "Oracle" };

            string[] ds4 = { "apricot", "new", "XML" };

            Console.WriteLine("Using standard comparer");
            Console.WriteLine("DS1 == DS2? {0}", ds1.SequenceEqual(ds2));
            Console.WriteLine("DS1 == DS3? {0}", ds1.SequenceEqual(ds3));
            Console.WriteLine("DS1 == DS4? {0}", ds1.SequenceEqual(ds4));

            MyComparer comparer = new MyComparer();

            Console.WriteLine("\nUsing custom comparer");
            Console.WriteLine("DS1 == DS2? {0}", ds1.SequenceEqual(ds2, comparer));
            Console.WriteLine("DS1 == DS3? {0}", ds1.SequenceEqual(ds3, comparer));
            Console.WriteLine("DS1 == DS4? {0}", ds1.SequenceEqual(ds4, comparer));

        }
    }
    class MyComparer : IEqualityComparer<string>
    {
        public bool Equals(string first, string second)
        {
            return first[0] == second[0];
        }

        public int GetHashCode(string str)
        {
            return str[0].GetHashCode();
        }
    }

Result


Related Tutorials