private static void PairDelegate() { //practice with Delegates //CREATE TWO Students and two dogs Student Jesse = new Student("Jesse"); Student Stacey = new Student("Stacey"); Dog Milo = new Dog(50); Dog Fred = new Dog(56); Pair theStudentPair = new Pair(Jesse, Stacey); Pair theDogPair = new Pair(Milo, Fred); Console.WriteLine($"student pair {theStudentPair}"); Console.WriteLine($"the dog pair {theDogPair}"); //Instantiate the delegates Pair.WhichIsFirst theStudentDelegate = new Pair.WhichIsFirst(Student.WhichStudentComesFirst); Pair.WhichIsFirst theDogDelegate = new Pair.WhichIsFirst(Dog.WhichDogComesFirst); //sort using the delegates theStudentPair.Sort(theStudentDelegate); theDogPair.Sort(theDogDelegate); Console.WriteLine($"After Sort the student pair {theStudentPair}"); Console.WriteLine($"After Sort the dog pair {theDogPair}"); theStudentPair.ReverseSort(theStudentDelegate); theDogPair.ReverseSort(theDogDelegate); Console.WriteLine($"After Reverse Sort the student pair {theStudentPair}"); Console.WriteLine($"After Reverse Sort the dog pair {theDogPair}"); Pair theEmployeePair = new Pair(new Employee(2), new Employee(4)); Pair.WhichIsFirst theEmpDelegate = new Pair.WhichIsFirst(Employee.WhichEmployeeComesFirst); theEmployeePair.Sort(theEmpDelegate); //create the delegate for reversing the name Pair.ReverseMyName reverseDelegateStudent = new Pair.ReverseMyName(Student.ReverseName); Pair.ReverseMyName reverseDelegateEmployee = new Pair.ReverseMyName(Employee.ReverseName); theStudentPair.ReverseObject(reverseDelegateStudent); theEmployeePair.ReverseObject(reverseDelegateEmployee); }
static void Main(string[] args) { Student Nam = new Student("Nam"); Student Hanh = new Student("Hanh"); Cat Mun = new Cat(5); Cat Muop = new Cat(3); Pair studentPair = new Pair(Nam, Hanh); Pair catPair = new Pair(Mun, Muop); Console.WriteLine("Sinh vien: \t\t\t {0}", studentPair.ToString()); Console.WriteLine("Cat: \t\t {0}", catPair.ToString()); Pair.WhichIsFirst theDelegate1 = new Pair.WhichIsFirst(Student.WhichStudentComesFirst); Pair.WhichIsFirst theDelegate2 = new Pair.WhichIsFirst(Cat.WhichCatComesFirst); Console.ReadKey(); }
static void Main(string[] args) { // tao hai doi tuong Student va Cat // dua chung vao hai doi tuong Pair Student Thao = new Student("Thao"); Student Mai = new Student("Mai"); Cat Mun = new Cat(5); Cat Ngao = new Cat(2); Pair studentPair = new Pair(Thao, Mai); Pair catPair = new Pair(Mun, Ngao); Console.WriteLine("Sinh vien \t\t: {0}", studentPair.ToString()); Console.WriteLine("Meo \t\t\t: {0}", catPair.ToString()); // tao uy quyen Pair.WhichIsFirst theStudentDelegate = new Pair.WhichIsFirst(Student.WhichStudentComesFirst); Pair.WhichIsFirst theCatDelegate = new Pair.WhichIsFirst(Cat.WhichCatComesFirst); // sap xep dung uy quyen studentPair.Sort(theStudentDelegate); Console.WriteLine("Sau khi sap xep studentPair\t\t: {0}", studentPair.ToString()); //studentPair.ReverseSort(theStudentDelegate); // Thuc hien uy quyen ma khong can khai bao the hien uy quyen cuc bo studentPair.ReverseSort(Student.OrderStudents); Console.WriteLine("Sau khi sap xep nguoc studentPair\t\t: {0}", studentPair.ToString()); catPair.Sort(theCatDelegate); Console.WriteLine("Sau khi sap xep catPair\t\t: {0}", catPair.ToString()); //catPair.ReverseSort(theCatDelegate); // Thuc hien uy quyen ma khong can khai bao the hien uy quyen cuc bo catPair.ReverseSort(Cat.OrderCats); Console.WriteLine("Sau khi sap xep nguoc catPair\t\t: {0}", catPair.ToString()); Console.ReadLine(); }