Namespace Changestringlib//public class {publicly class Class1 { //public void changestring (string[] str) //{ //For (int i = 0; i < str. Length; i++) // { // str[i] = str[i]. ToUpper (); } //} //public method publicly void changestring (string[] str, chgstrdelegate chgstr) {for ( int i = 0; I < Str. Length; i++) { //delegate, just extract the changes in the method, easy to write common code str[i] = Chgstr (Str[i]); }}}
Delegate class
Namespace changestringlib{public delegate string chgstrdelegate (String str);}
Invoke Item
Namespace consoleapplication1{ class program { static void Main (string[] args) { Changestringlib.class1 C1 = new Changestringlib.class1 (); String[] name = {"Bob", "Chris", "James", "Steve", "Jhoney"}; Turn the strings into uppercase //c1. Changestring (name,changestringtoupper); C1. Changestring (name, CHANGECHANGEADDWJX); foreach (var item in name) { Console.WriteLine (item); } Console.WriteLine ("OK"); } static string Changechangeaddwjx (String msg) { return "★" + MSG + "★"; } static string Changestringtoupper (String msg) { return MSG. ToUpper ();}}}
Delegate Case 1 Variable pull out