码迷,mamicode.com
首页 > 其他好文 > 详细

委托错误点

时间:2014-06-13 14:50:06      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 委托练习
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = Console.ReadLine();
13             if (s == "1")
14             {
15                 SomeDelegate f = Hello;
16             }
17             else
18             {
19                 SomeDelegate f = Wow;
20             }
21       //两处错误:1、没有调用方法。2、变量f的作用域
22             Console.Read();
23         }
24 
25         static void Hello(string name)
26         {
27             Console.WriteLine("Hello:{0}", name);
28         }
29 
30         static void Wow(string name)
31         {
32             Console.WriteLine("Wow:{0}", name);
33         }
34     }
35 
36     delegate void SomeDelegate(string name);
37 }
bubuko.com,布布扣

正解::

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 委托练习
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = Console.ReadLine();
13 
14             SomeDelegate f;//解决问题2,变量作用域
15             if (s == "1")
16             {
17                 f = Hello;//只是指向,没有执行
18             }
19             else
20             {
21                 f = Wow;
22             }
23             f("aaa");//调用f指向的函数//解决问题一,方法调用
24             Console.Read();
25         }
26 
27         static void Hello(string name)
28         {
29             Console.WriteLine("Hello:{0}", name);
30         }
31 
32         static void Wow(string name)
33         {
34             Console.WriteLine("Wow:{0}", name);
35         }
36     }
37 
38     delegate void SomeDelegate(string name);
39 }
bubuko.com,布布扣

 

委托错误点,布布扣,bubuko.com

委托错误点

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/skyl/p/3785141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!