static void Main(string[] args) { SimpleDelegate sd = new SimpleDelegate(); // delegate ساخت نمونهاي جديد از Comparer cmp = new Comparer(Name.CompareFirstNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); }
static void Main(string[] args) { SimpleDelegate sd = new SimpleDelegate(); // this is the delegate instantiation Comparer cmp = new Comparer(Name.CompareFirstNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); // observe the delegate argument sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); }
public static void MainMethod() { var sd = new SimpleDelegate(); //To use a delegate, you must create an instance of it. // this is the delegate instantiation Comparer cmp = new Comparer(Name.CompareFirstNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); // observe the delegate argument sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); /* The delegate, cmp, is then used as a parameter to the Sort() method, which uses it just like a normal method. * Observe the way the delegate is passed to the Sort() method as a parameter in the code below. * * sd.Sort(cmp); * * Using this technique, any delegate handler method may be passed to the Sort() method at run-time. i.e. * You could define a method handler named CompareLastNames(), instantiate a new Comparer delegate instance with it, * and pass the new delegate to the Sort() method. */ cmp = new Comparer(Name.CompareLastNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); // observe the delegate argument sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); }
static void xMain(string[] args) { SimpleDelegate sd = new SimpleDelegate(); // this is the delegate instantiation Comparer cmp = new Comparer(Name.CompareFirstNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); // observe the delegate argument sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); // keep screen from going away // when run from VS.NET Console.ReadLine(); }