Monday, March 4, 2013

Dictionary method with IEqualityComparer method


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BInnovitec.Abstract
{
    public class Employee
    {
        public string rollNumber { get; set; }
        public string name { get; set; }
        public string branch { get; set; }

        public static ArrayList GetEmployeeArrayList()
        {
            ArrayList alEmployee = new ArrayList();
            alEmployee.Add(new Employee { rollNumber = "1", name = "Girijesh", branch = "CS" });
            alEmployee.Add(new Employee { rollNumber = "2", name = "Srijesh", branch = "ME" });
            return alEmployee;
        }
        public static Employee[] GetEmployeeArray()
        {
            //their are 2methods defined here use anyone.
            return (GetEmployeeArrayList().Cast<Employee>().ToArray());
            //return ((Employee[])GetEmployeeArrayList().ToArray(typeof(Employee)));
        }
    }

    public class MyStringFieldNumberComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            return (Int32.Parse(x) == Int32.Parse(y));
        }
        public int GetHashCode(string obj)
        {
            return Int32.Parse(obj).ToString().GetHashCode();
        }
    }
    //refrence from java2s.com
    public class Program
    {
        static void Main(string[] args)
        {
            CompleExampleOfArrayListWithDictionary();
            Console.ReadKey();
        }

        private static void CompleExampleOfArrayListWithDictionary()
        {
            Dictionary<String, Employee> employeeArrayDataRecord = Employee.GetEmployeeArray().ToDictionary(k => k.rollNumber, new MyStringFieldNumberComparer());
            string[] userInput = { "01", "0001", "000001","000000001 " };
            foreach (string input in userInput)
            {
                Employee e = employeeArrayDataRecord[input];
                Console.WriteLine("Employee whose rollnumber is == \"{0}\" : {1} {2}", input, e.name, e.branch);
            }
        }
    }
}

==================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BInnovitec.Abstract
{
    public class Employee
    {
        public int rollNumber { get; set; }
        public string name { get; set; }
        public string branch { get; set; }

        public static ArrayList GetEmployeeArrayList()
        {
            ArrayList alEmployee = new ArrayList();
            alEmployee.Add(new Employee { rollNumber = 1, name = "Girijesh", branch = "CS" });
            alEmployee.Add(new Employee { rollNumber = 2, name = "Srijesh", branch = "ME" });
            return alEmployee;
        }
        public static Employee[] GetEmployeeArray()
        {
            //their are 2methods defined here use anyone.
            return (GetEmployeeArrayList().Cast<Employee>().ToArray());
            //return ((Employee[])GetEmployeeArrayList().ToArray(typeof(Employee)));
        }
    }

    public class MyStringFieldNumberComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            return (Int32.Parse(x) == Int32.Parse(y));
        }
        public int GetHashCode(string obj)
        {
            return Int32.Parse(obj).ToString().GetHashCode();
        }
    }  
    public class Program
    {
        static void Main(string[] args)
        {
            CompleExampleOfArrayListWithDictionary();
            Console.ReadKey();
        }

        private static void CompleExampleOfArrayListWithDictionary()
        {
            Dictionary<int, string> employeeArrayDataRecord = Employee.GetEmployeeArray().ToDictionary(k => k.rollNumber, i => string.Format("{0}, {1}", i.name, i.branch));
            int[] userInput = { 1,2 };
            foreach (int input in userInput)
            {
                string name = employeeArrayDataRecord[input];
                Console.WriteLine("Employee information in string format is ={0}", name);
            }
        }
    }
}




No comments:

Post a Comment