March 2004
Intermediate to advanced
560 pages
26h 47m
English
using System; using System.Collections; namespace Samples { public class IComparerSample { public class ToStringComparer: IComparer { public int Compare(Object a, Object b) { string s1 = a.ToString(); string s2 = b.ToString(); return s1.CompareTo(s2); } } public static void Main() { ArrayList a = new ArrayList(); a.Add (0); a.Add("1"); a.Add(2); a.Add(3); a.Add("4"); a.Add(5); a.Add("6"); SearchFor(a, "2"); SearchFor(a, 1); SearchFor(a, 9); } public static void SearchFor(ArrayList a, object o) { int i = a.BinarySearch(o, new ToStringComparer()); if(i > 0) { Console.WriteLine( "Object: {0}, was found at: {1}", o, i ); } else if(~i == a.Count) { Console.WriteLine("Object: {0}, was not found", o); Console.WriteLine( "No object in the array ...Read now
Unlock full access