C# 一个人可以写一个方法来利用Lambda表达式吗?

C# 一个人可以写一个方法来利用Lambda表达式吗?,c#,C#,我以前从未使用过Lambda表达式,希望能够编写自己的方法来使用Lambda表达式。有没有办法做到这一点,如果有,为什么我的方法在下面不起作用 问题:我的testString方法能否像我的p.Zip示例那样接受lambda表达式 代码: -这就是我不确定为什么不使用lambda表达式甚至 如果它应该能工作的话 添加了新代码,以便查看是否可以根据下面的答案调用委托函数。它的一部分工作,但如果我想有几个函数,满足delagates参数列表呢 类别: using System; using Syste

我以前从未使用过Lambda表达式,希望能够编写自己的方法来使用Lambda表达式。有没有办法做到这一点,如果有,为什么我的方法在下面不起作用

问题:我的testString方法能否像我的p.Zip示例那样接受lambda表达式

代码:

-这就是我不确定为什么不使用lambda表达式甚至

如果它应该能工作的话 添加了新代码,以便查看是否可以根据下面的答案调用委托函数。它的一部分工作,但如果我想有几个函数,满足delagates参数列表呢

类别:

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

namespace DDHTestDelegate
{
    class Program
    {
        // declare a delegate that takes in two strings and returns a string:
        delegate string StringDelegate(string inputString, string baseString);

        public void someFunction(string a, string b)
        {
            Console.WriteLine("String a: " + a);
            Console.WriteLine("String a: " + b);
        }

        // this calls the delegate function
        static string Test(StringDelegate theFunction, string inputString1, string inputString2)
        {
            return theFunction(inputString1, inputString2);
        }


        static void Main(string[] args)
        {
            Console.WriteLine(Test((input1, input2) => input1 + ": " + input2, "S1", "S2"));
            Console.ReadKey();
        }
    }
}
这是我认为让我感到困惑的一部分,这正是我现在想要做的

   (input1, input2) => input1 + ": " + input2    
由于StringDelegate函数只返回值,因此如何在测试函数中使用此代码。这将编译并运行,但我不能完全确定它为什么会这样工作

更多代码:

         Console.WriteLine(Test(someFunction("w","i")), 
                          "S1", 
                          "S2");
为什么这个代码也不起作用。测试函数包含三个参数。1一个函数,其他两个字符串。为什么会产生问题呢

其他例子:

    static public void testString(Func<string, string, string> function)
    {
       temp1 = "a";
       temp2 = "b";
       Console.WriteLine(function(a, b));
    }

    testString((temp1, temp2) => temp1 + " -- " + temp2);

通常,可以将lambda或委托作为方法参数。最简单的方法是使用Action和Func类型


通常,可以将lambda或委托作为方法参数。最简单的方法是使用Action和Func类型


这并不总是像Marcin发布的那样容易编写,但您可以显式声明委托,如:

// declare a delegate that takes in two strings and returns a string:
delegate string StringDelegate(string inputString, string baseString);
然后定义使用此委托的函数:

// this calls the delegate function
static string Test(StringDelegate theFunction, string inputString1, string inputString2)
{
    return theFunction(inputString1, inputString2);
}
最后,您可以将其用于lambda,如:

Console.WriteLine(Test((input1, input2) => input1 + ": " + input2, "S1", "S2"));

同样,可能一开始并不像Func选项那么简单,但是如果您想将委托与使用这些委托的函数分开定义,它可能会很有用。

这并不总是像Marcin发布的那样容易编写,但您可以显式声明委托,如:

// declare a delegate that takes in two strings and returns a string:
delegate string StringDelegate(string inputString, string baseString);
然后定义使用此委托的函数:

// this calls the delegate function
static string Test(StringDelegate theFunction, string inputString1, string inputString2)
{
    return theFunction(inputString1, inputString2);
}
最后,您可以将其用于lambda,如:

Console.WriteLine(Test((input1, input2) => input1 + ": " + input2, "S1", "S2"));

再说一次,可能一开始不像Func选项那么简单,但是,如果您希望将委托与使用这些委托的函数分开定义,则它可能会很有用。

当我在测试程序中尝试此操作时,代码中会出现各种错误。您可以粘贴一个工作模型作为示例。当我在测试程序中尝试此操作时,代码中会出现各种错误,您可以粘贴一个以工作模型为例。
// this calls the delegate function
static string Test(StringDelegate theFunction, string inputString1, string inputString2)
{
    return theFunction(inputString1, inputString2);
}
Console.WriteLine(Test((input1, input2) => input1 + ": " + input2, "S1", "S2"));