2013-05-01 73 views
0

我想要使用Lambda表達式,但在下面註釋的行中出現錯誤,當我嘗試調用它時。嘗試調用它時出現Lambda表達式錯誤

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace ConsoleAppTestDelegate2 
    { 
    public delegate string MyDelegate (int a); 
    public class ClassRunDelegate 
    { 
     public void RunDelegate(MyDelegate a, int b) 
     { 
      Console.WriteLine(a(b)); 
     } 
    } 

    public class MyHelp 
    { 
     public string test(int a) 
     { 
      a++; 
      return a.ToString(); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      MyHelp fhelp = new MyHelp(); 
      // 
      MyDelegate fdelegate = new MyDelegate(fhelp.test); 
      ClassRunDelegate cc = new ClassRunDelegate(); 
      cc.RunDelegate(fdelegate, 10);    
      /// 
      cc.RunDelegate((a, b) => { Console.WriteLine("test"); });// get error this line 
      Console.ReadLine(); 

      } 
     } 
    } 
+0

錯誤說的是什麼? – 2013-05-01 11:16:57

+0

該行甚至不是有效的。我不知道你在做什麼。 – Justin 2013-05-01 11:17:57

回答

1

從你的代碼,MyDelegate應該返回字符串,但Console.WriteLine("test")不返回任何東西,所以這並不編譯:

cc.RunDelegate((a) => { Console.WriteLine("test"); }, b); 

Console.WriteLine後,您應該返回的東西,或使用其他類型的委託,沒有回報價值。

+0

感謝您的回答 – khoshghadam 2013-05-01 11:33:33

相關問題