代表工作不像我认为它应该是(Lambda Expression不工作)?

时间:2014-03-27 20:03:05

标签: c#

以下代码是一个简单Lambda表达式的示例,如果我使用Lambda表达式创建一个function1但是如果不创建它则不会编译。从有人告诉我的另一个问题(不完全相同),编译将为你生成一些代码,就像在线一样。

问题:我不确定参数列表的构成和Lambda表达式的实际代码是什么?" (我认为这可能是导致我麻烦的原因)

示例:

  Console.WriteLine(Test( (input1, input2, input3, input4) => input1 + 
                                                                  " -- " + 
                                                                  input2 + 
                                                                  " -- " +
                                                                  input3 + 
                                                                  " -- " +
                                                                  input4, 
                                                                  "look at this", 
                                                                  "How do you do!");

代码:

      using System.Text;

namespace DDHTestDelegate
{
    class Program
    {
        delegate string StringDelegate(string inputString, 
                                       string baseString, 
                                       string secondaryBaseString, 
                                       string commonBaseString);

        delegate int IntegerDeleage(int inputInt);

        StringDelegate function1 = (input1, input2, input3, input4) => input1 + "--" + 
                                                                       input2 + "-- " +
                                                                       input3 + "--" +
                                                                       input4;
        StringDelegate function2 = (a, b, c, d) => a + b + c + d;
        StringDelegate function3 = (a, b, c, d) => a + b + " ---- " + c + d;

        static string Test(StringDelegate theFunction, 
                           string inputString1, 
                           string inputString2, 
                           string inputString3, 
                           string inputString4)
        {
            return theFunction(inputString1, inputString2, inputString3, inputString4);
        }

        static void Main(string[] args)
        {
           Program v = new Program();
           Console.WriteLine(Test(v.function1, "S1", "S2", "look at this", "How do you do!"));
           Console.WriteLine(Test(v.function2, "S1", "S2", "look at this", "How do you do!"));
           Console.WriteLine(Test(v.function3, "AAA", "BBB", "CCC", "DDD"));
           Console.WriteLine("----");
           Console.WriteLine(Test( (input1, input2, input3, input4) => input1 + 
                                                                      " -- " + 
                                                                      input2 + 
                                                                      " -- " +
                                                                      input3 + 
                                                                      " -- " +
                                                                      input4, 
                                                                      "look at this", 
                                                                      "How do you do!");

           Console.ReadKey();
        }
    }
}

弄清楚我的错误。我在=>之后计算了值。作为直到第一个逗号的参数,但实际上参数列表直到第一个逗号后才开始。

工作代码:

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

namespace DDHTestDelegate
{
    class Program
    {
        delegate string StringDelegate(string inputString, 
                                       string baseString, 
                                       string secondaryBaseString, 
                                       string commonBaseString);

        delegate int IntegerDeleage(int inputInt);

        StringDelegate function1 = (input1, input2, input3, input4) => input1 + "--" + 
                                                                       input2 + "-- " +
                                                                       input3 + "--" +
                                                                       input4;
        StringDelegate function2 = (a, b, c, d) => a + b + c + d;
        StringDelegate function3 = (a, b, c, d) => a + b + " ---- " + c + d;

        static string Test(StringDelegate theFunction, 
                           string inputString1, 
                           string inputString2, 
                           string inputString3, 
                           string inputString4)
        {
            return theFunction(inputString1, inputString2, inputString3, inputString4);
        }

        static void Main(string[] args)
        {
           Program v = new Program();

           Console.WriteLine(Test(v.function1, "S1", "S2", "look at this", "How do you do!"));
           Console.WriteLine(Test(v.function2, "S1", "S2", "look at this", "How do you do!"));
           Console.WriteLine(Test(v.function3, "AAA", "BBB", "CCC", "DDD"));

           Console.WriteLine("----");

           Console.WriteLine(Test( (input1, input2, input3, input4) => input1 + " -- " + input2 + " -- " +  input3 + " -- " + "You eat MACs!" + input4, 
                                                                       "Parm1", 
                                                                       "Parm2",
                                                                       "Parm3", 
                                                                       "Parm4"));

           Console.ReadKey();
        }


    }
}

2 个答案:

答案 0 :(得分:0)

这不是lambda表达式的问题。您的Test方法需要5个参数,1个代表和4个字符串,但您只提供了一个委托和2个字符串。

尝试再提供2个,它将起作用:

Console.WriteLine(Test( (input1, input2, input3, input4) => ..., 
                        "look at this", 
                        "How do you do!", 
                        "another string", 
                        "yet another string")

答案 1 :(得分:0)

问题是您没有使用lambda表达式在调用中指定足够的参数。将其更改为

Console.WriteLine(Test( (input1, input2, input3, input4) => input1 + 
                                                              " -- " + 
                                                              input2 + 
                                                              " -- " +
                                                              input3 + 
                                                              " -- " +
                                                              input4, 
                                                              "S1", 
                                                              "S2", 
                                                              "look at this", 
                                                              "How do you do!");

我已将"S1""S2"添加到参数列表中。