using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Collections.ObjectModel; //csc List_BinarySearchType1.cs namespace ConsoleApplication1 { //class ObjectFacotry : IComparable public class ObjectCompare :IComparer { public int Compare(string str1, string str2) // { return str1.CompareTo(str2); } } class Program { static void Main(string[] args) { List eList = new List(); eList.Add("A"); eList.Add("B"); eList.Add("C"); eList.Add("D"); eList.Add("E"); readT(eList); ObjectCompare oc = new ObjectCompare(); int n1 = oc.Compare("A", "A"); if (n1 == 0) { Console.WriteLine("Strings Matched"); } else { Console.WriteLine("Strings did not Match"); } eList.Sort(oc); SearchInsert(eList, "M"); readT(eList); // Console.ReadLine(); } static void SearchInsert(List list1, string insert1) { int index = list1.BinarySearch(insert1); if (index < 0) { list1.Insert(~index, insert1); } } static void readT(IList list) { //notice that the list is passed an IList and hence can handle both types of lists //Use IEnumerable foreach (string val in list) { Console.WriteLine("IList Value:" + val.ToString()); } } } }