标签:
1 namespace DelegateDemo 2 { 3 //声明委托 4 public delegate void MyDel(); 5 //声明带参的委托 6 public delegate void MyDelAdd(int num1, int num2); 7 //声明带有返值的委托 8 public delegate string MyDelStrUp(string s); 9 10 //声明委托用于演示匿名方法 11 public delegate string ProcessString(string s); 12 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 #region 委托演示 18 TestDel t = new TestDel(); 19 20 #region 简单实例化委托与调用委托 21 Console.WriteLine("-----以下是简单使用委托演示--------"); 22 //t.MyMethod(); 23 ///实例化委托,用一个方法来进行实例化 24 ///该方法签名要与委托签名一致 25 MyDel del = new MyDel(t.MyMethod); 26 27 ///调用委托 28 del(); 29 30 //C#2.0后可以这种方式实例化委托 31 MyDel del4 = t.MyMethod; 32 del4(); 33 34 //用静态方法进行实例化 35 del4 = TestDel.MyStaticMethod; 36 del4(); 37 38 //以下代码效果相同 39 // MyDelAdd del2 = new MyDelAdd(t.MyMethod); 40 //del2(10, 20); 41 MyDelAdd del2 = t.MyMethod; 42 del2(10, 20); 43 44 //MyDel3 del3 = new MyDel3(t.MyMethod); 45 //Console.WriteLine(del3("abc")); 46 #endregion 47 48 #region 匿名方法实例化委托 49 Console.WriteLine("-----以下是匿名方法演示--------"); 50 //用匿名方法实例化委托 51 ProcessString p = delegate(string inputString) { 52 return inputString.ToUpper(); 53 }; 54 //通过委托调用匿名方法 55 Console.WriteLine(p("aaaa")); 56 #endregion 57 58 #region 委托多播演示 59 60 Console.WriteLine("-----以下是委托多播演示--------"); 61 MyDel mydel1 = t.MyMethod; 62 MyDel mydel2 = t.MyMethod2; 63 MyDel mydel3 = TestDel.MyMethod3; 64 MyDel allMyDel = mydel1 + mydel2 + mydel3; 65 allMyDel(); 66 Console.WriteLine("----- allMyDel -= mydel3;多播演示--------"); 67 allMyDel -= mydel3; 68 allMyDel(); 69 70 #endregion 71 72 #region 委托作为参数演示 73 74 Console.WriteLine("-------以下是委托作为参数演示------"); 75 MyDelStrUp paramMyDel3 = t.MyMethod; 76 TestDel.MyParamMethod("aaa", paramMyDel3); 77 78 #endregion 79 80 #region 委托作为返回值 81 82 Console.WriteLine("---以下是委托作为返回值演示------"); 83 ///returnMyDel指向t.MyReturnMethod()的返回值 84 MyDelStrUp returnMyDel = t.MyReturnMethod(); 85 ///returnMyDel指向t.MyMethod 86 //MyDel3 returnMyDel = t.MyMethod; 87 88 Console.WriteLine(returnMyDel("sssssssssssss")); 89 90 #endregion 91 92 #endregion 93 94 //MyReturnDelegateTest my = new MyReturnDelegateTest(); 95 //my.MyTest(); 96 MyParamDelegateTest myParam = new MyParamDelegateTest(); 97 myParam.AddBooks(); 98 myParam.MyTest(); 99 Console.Read(); 100 } 101 } 102 /// <summary> 103 ///这是 类,有好多方法 104 /// </summary> 105 public class TestDel 106 { 107 #region 普通方法 108 /// <summary> 109 /// 静态方法 110 /// </summary> 111 public static void MyStaticMethod() 112 { 113 Console.WriteLine("My Static Method"); 114 } 115 /// <summary> 116 /// 输出 mymethod 117 /// </summary> 118 public void MyMethod() 119 { 120 Console.WriteLine("MyMethod"); 121 } 122 public void MyMethod2() 123 { 124 Console.WriteLine("My Method 22222222222"); 125 } 126 /// <summary> 127 /// 静态函数 128 /// </summary> 129 public static void MyMethod3() 130 { 131 Console.WriteLine("My Method 3333333333333"); 132 } 133 /// <summary> 134 /// 返回两个数的和 135 /// </summary> 136 /// <param name="num1"></param> 137 /// <param name="num2"></param> 138 public void MyMethod(int num1, int num2) 139 { 140 Console.WriteLine(num1 + num2); 141 } 142 /// <summary> 143 /// 字符串变成大写 144 /// </summary> 145 /// <param name="s"></param> 146 /// <returns></returns> 147 public string MyMethod(string s) 148 { 149 return s.ToUpper(); 150 } 151 #endregion 152 153 /// <summary> 154 /// 委托作为方法参数 155 /// </summary> 156 /// <param name="s"></param> 157 /// <param name="del3"></param> 158 public static void MyParamMethod(string s, MyDelStrUp del3) 159 { 160 Console.WriteLine(del3(s)); 161 } 162 163 /// <summary> 164 /// 委托作为返回值 165 /// </summary> 166 /// <param name="s"></param> 167 /// <returns></returns> 168 public MyDelStrUp MyReturnMethod() 169 { 170 ///返回符合委托规范的方法 171 return MyMethod; 172 } 173 } 174 175 // 委托作为参数 176 177 public class MyParamDelegateTest 178 { 179 BookDB bookDB = new BookDB(); 180 public void AddBooks() 181 { 182 bookDB.AddBook(new Book() { BookID=1,BookName="C#",Price=123,IsPaperbook=true }); 183 bookDB.AddBook(new Book() { BookID = 1, BookName = "C#", Price = 123, IsPaperbook = false }); 184 bookDB.AddBook(new Book() { BookID = 2, BookName = "ASP.Net", Price = 12, IsPaperbook = true }); 185 bookDB.AddBook(new Book() { BookID = 1, BookName = "ADO", Price = 23, IsPaperbook = false }); 186 } 187 /// <summary> 188 /// 用来实例化委托 189 /// </summary> 190 /// <param name="b"></param> 191 public void TestProcessBook(Book b) 192 { 193 if (b.IsPaperbook) 194 { 195 Console.WriteLine(b.BookName); 196 } 197 } 198 double total = 0; 199 public void TotalPrice(Book b) 200 { 201 total += b.Price; 202 } 203 public void MyTest() 204 { 205 206 //ProcessBook p=TestProcessBook; 207 //ProcessBook p1=TotalPrice; 208 //ProcessBook p2=p+p1; 209 //把方法名做为参数进行传递 210 bookDB.PrintBook(TestProcessBook); 211 bookDB.PrintBook(TotalPrice); 212 Console.WriteLine(total); 213 } 214 215 216 } 217 /// <summary> 218 /// 委托 219 /// </summary> 220 /// <param name="b"></param> 221 public delegate void ProcessBook(Book b); 222 223 public class BookDB 224 { 225 public List<Book> books = new List<Book>(); 226 public void AddBook(Book b) 227 { 228 books.Add(b); 229 } 230 /// <summary> 231 /// 委托做参数 232 /// </summary> 233 /// <param name="process">委托</param> 234 public void PrintBook(ProcessBook process) 235 { 236 foreach (var book in books) 237 { 238 //这里进行委托传进来的函数 操作book 239 process(book); 240 } 241 } 242 } 243 244 public class Book 245 { 246 public int BookID { get; set; } 247 public string BookName { get; set; } 248 public double Price { get; set; } 249 public bool IsPaperbook { get; set; } 250 } 251 252 // 委托作为返回值 253 254 public delegate int MyReturnDelegate(int num1, int num2); 255 256 public class MyReturnDelegateTest 257 { 258 public void MyTest() 259 { 260 MyCalcuate myCalcuate = new MyCalcuate(); 261 do 262 { 263 Console.WriteLine("请输入符号进行以计算( + - * /)"); 264 string oper = Console.ReadLine(); 265 Console.WriteLine("请输入操作数1"); 266 string num1 = Console.ReadLine(); 267 268 Console.WriteLine("请输入操作数2"); 269 string num2 = Console.ReadLine(); 270 271 MyReturnDelegate myReturn = myCalcuate.Calcuate(oper); 272 int result = myReturn(int.Parse(num1), int.Parse(num2)); 273 Console.WriteLine( 274 string.Format("{0}{1}{2}={3}", num1,oper,num2, result)); 275 276 Console.WriteLine("您还要继续吗?Y/N"); 277 //string continueFlag = Console.ReadLine(); 278 //if (continueFlag.ToUpper() == "N") break; 279 280 } while (Console.ReadLine().ToUpper()!="N"); 281 } 282 } 283 284 public class MyCalcuate 285 { 286 public MyReturnDelegate Calcuate(string oper) 287 { 288 MyReturnDelegate myReturn = null; 289 switch (oper) 290 { 291 case "+": 292 myReturn = delegate(int num1, int num2) { return num1 + num2; }; 293 break; 294 case "-": 295 myReturn = delegate(int num1, int num2) { return num1 - num2; }; 296 break; 297 case "*": 298 myReturn = delegate(int num1, int num2) { return num1 * num2; }; 299 break; 300 case "/": 301 myReturn = delegate(int num1, int num2) { return num1 / num2; }; 302 break; 303 default: 304 break; 305 } 306 return myReturn; 307 } 308 } 309 }
标签:
原文地址:http://www.cnblogs.com/nanxiaoxiang/p/5093100.html