Delegate

500 views
413 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
500
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Delegate

  1. 1. Delegate
  2. 2. Delegate • A delegate provides a way to encapsulate a method. • An event is a notification that some action has occurred. • Delegates and events are related because an event is built upon a delegate.
  3. 3. • A delegate is an object that can refer to a method. • when you create a delegate, you are creating an object that can hold a reference to a method. • Furthermore, the method can be called through this reference. • A delegate can invoke the method to which it refers. • A delegate in C# is similar to a function pointer in C/C++.
  4. 4. • A delegate type is declared using the keyword delegate. delegate return-type name (parameter-list ); • return-type is the type of value returned by the methods that the delegate will be calling. • The name of the delegate is specified by name. • The parameters required by the methods called through the delegate are specified in the parameter-list.
  5. 5. • A delegate instance (Object) can refer to and call methods whose return type and parameter list match those specified by the delegate declaration. • The method can be either an instance method associated with an object or a static method associated with a class.
  6. 6. using System; delegate string StrMod(string str); class DelegateTest { static string ReplaceSpaces(string s) { Console.WriteLine("Replacing spaces with hyphens."); return s.Replace(' ', '-'); }
  7. 7. static string RemoveSpaces(string s) { string temp = ""; int i; Console.WriteLine("Removing spaces.") for(i=0; i < s.Length; i++) if(s[i] != ' ') temp += s[i]; return temp; }
  8. 8. static string Reverse(string s) { string temp = ""; int i, j; Console.WriteLine("Reversing string."); for(j=0, i=s.Length-1; i >= 0; i--, j++) temp += s[i]; return temp; }
  9. 9. static void Main() { StrMod strOp = new StrMod(ReplaceSpaces); string str; str = strOp("This is a test."); Console.WriteLine("Resulting string: " + str); Console.WriteLine(); strOp2 = new StrMod(RemoveSpaces); str = strOp2("This is a test."); Console.WriteLine("Resulting string: " + str);
  10. 10. strOp = new StrMod(Reverse); str = strOp("This is a test."); Console.WriteLine("Resulting string: " + str); } } //output Replacing spaces with hyphens. Resulting string: This-is-a-test. Removing spaces. Resulting string: Thisisatest. Reversing string. Resulting string: .tset a si sihT
  11. 11. Delegate Method Group Conversion • Since version 2.0, C# has included an option that significantly simplifies the syntax that assigns a method to a delegate. • This feature is called method group conversion, and it allows you to simply assign the name of a method to a delegate, without using new or explicitly invoking the delegate’s constructor.
  12. 12. // use method group conversion StrMod strOp = ReplaceSpaces; string str; // Call methods through the delegate. str = strOp("This is a test."); Console.WriteLine("Resulting string: " + str); Console.WriteLine();
  13. 13. Instance Methods as Delegates delegate string StrMod(string str); class StringOps { public string ReplaceSpaces(string s) { Console.WriteLine("Replacing spaces with hyphens."); return s.Replace(' ', '-'); }
  14. 14. public string RemoveSpaces(string s) { string temp = ""; int i; Console.WriteLine("Removing spaces."); for(i=0; i < s.Length; i++) if(s[i] != ' ') temp += s[i]; return temp; }
  15. 15. public string Reverse(string s) { string temp = ""; int i, j; Console.WriteLine("Reversing string."); for(j=0, i=s.Length-1; i >= 0; i--, j++) temp += s[i]; return temp; } } class DelegateTest { static void Main() { StringOps so = new StringOps();
  16. 16. // Initialize a delegate. StrMod strOp = so.ReplaceSpaces; string str; // Call methods through delegates. str = strOp("This is a test."); Console.WriteLine("Resulting string: " + str); Console.WriteLine();
  17. 17. strOp = so.RemoveSpaces; str = strOp("This is a test."); Console.WriteLine("Resulting string: " + str); Console.WriteLine(); strOp = so.Reverse; str = strOp("This is a test."); Console.WriteLine("Resulting string: " + str); }}
  18. 18. Multicasting • One of the most exciting features of a delegate is its support for multicasting. • Multicasting is the ability to create an invocation list, or chain, of methods that will be automatically called when a delegate is invoked. • Such a chain is very easy to create.
  19. 19. • Simply instantiate a delegate, and then use the + or += operator to add methods to the chain. • To remove a method, use – or – =. • If the delegate returns a value, then the value returned by the last method in the list becomes the return value of the entire delegate invocation. • Thus, a delegate that makes use of multicasting will often have a void return type.
  20. 20. using System; // Declare a delegate type. delegate void StrMod(ref string str); class MultiCastDemo { static void ReplaceSpaces(ref string s) { Console.WriteLine("Replacing spaces with hyphens."); s = s.Replace(' ', '-'); }
  21. 21. static void RemoveSpaces(ref string s) { string temp = ""; int i; Console.WriteLine("Removing spaces."); for(i=0; i < s.Length; i++) if(s[i] != ' ') temp += s[i]; s = temp; }
  22. 22. static void Reverse(ref string s) { string temp = ""; int i, j; Console.WriteLine("Reversing string."); for(j=0, i=s.Length-1; i >= 0; i--, j++) temp += s[i]; s = temp; }
  23. 23. static void Main() { StrMod strOp; StrMod replaceSp = ReplaceSpaces; StrMod removeSp = RemoveSpaces; StrMod reverseStr = Reverse; string str = "This is a test"; strOp = replaceSp; strOp += reverseStr; // Call multicast. strOp(ref str); Console.WriteLine("Resulting string: " + str);
  24. 24. // Remove replace and add remove. strOp -= replaceSp; strOp += removeSp; str = "This is a test."; // reset string // Call multicast. strOp(ref str); Console.WriteLine("Resulting string: " + str); Console.WriteLine(); } }
  25. 25. output • • • • • • Replacing spaces with hyphens. Reversing string. Resulting string: tset-a-si-sihT Reversing string. Removing spaces. Resulting string: .tsetasisihT
  26. 26. Covariance and Contravariance • here are two features that add flexibility to delegates: covariance and contravariance. Normally, the method that you pass to a delegate must have the same return type and signature as the delegate. • However, covariance and contravariance relax this rule slightly, as it pertains to derived types.
  27. 27. • Covariance enables a method to be assigned to a delegate when the method’s return type is a class derived from the class specified by the return type of the delegate. • Contravariance enables a method to be assigned to a delegate when a method’s parameter type is a base class of the class specified by the delegate’s declaration.
  28. 28. covariance and contravariance using System; class X { public int Val; } // Y is derived from X. class Y : X { } // This delegate returns X and takes a Y argument. delegate X ChangeIt(Y obj);
  29. 29. class CoContraVariance { static X IncrA(X obj) { X temp = new X(); temp.Val = obj.Val + 1; return temp; } static Y IncrB(Y obj) { Y temp = new Y(); temp.Val = obj.Val + 1; return temp; }
  30. 30. static void Main() { Y Yob = new Y(); ChangeIt change = IncrA; X Xob = change(Yob); Console.WriteLine("Xob: " + Xob.Val); change = IncrB; Yob = (Y) change(Yob); Console.WriteLine("Yob: " + Yob.Val); } }
  31. 31. • ChangeIt change = IncrA; • uses contravariance to enable IncrA( ) to be passed to the delegate because IncrA( ) has an X parameter, but the delegate has a Y parameter. This works because, with contravariance, if the parameter type of the method passed to a delegate is a base class of the parameter type used by the delegate, then the method and the delegate are compatible.
  32. 32. • The next line is also legal, but this time it is because of covariance: • change = IncrB; • In this case, the return type of IncrB( ) is Y, but the return type of ChangeIt is X. However, because the return type of the method is a class derived from the return type of the delegate, the two are compatible.
  33. 33. Anonymous Method

×