다음 코드는 람다 식으로 function1을 만들지 만 컴파일하지 않으면 작동하는 간단한 람다 식의 예제입니다. 다른 사람이이 질문과 비슷한 다른 질문 (정확하지 않음)에서 컴파일하면 컴파일 된 코드가 생성됩니다.대리인이 작동하지 않습니다 (Lambda Expression이 작동하지 않음).
질문 : 나는 매개 변수 목록 및 람다 식에 대한 실제 코드를 구성하는 것에 확실하지 않다 " (나는 그것이 나에게 문제의 원인을 것 같아요)
예 :
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();
}
}
}
을 알아 냈 내가이 때까지 최대 => A와 매개 변수 뒤에 값을 계산했다 첫 번째 쉼표가 있지만 실제로 매개 변수 목록은 첫 번째 쉼표 이후까지 시작되지 않습니다.
작업 코드 :
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();
}
}
}
어쩌면 매개 변수 목록이 델리게이트 람다 expresion에서 어떻게 작동하는지 모르겠습니다. (...) => parm1, parm2, ... parmN <-이() 괄호는 => 다음에 나오는 식입니다. –
@ user3376708 0 개 또는 여러 개의 매개 변수로 람다 식을 선언하려면 괄호 안에 매개 변수 목록을 래핑해야합니다 (예 :'(a, b, c, d) => ...). 람다식이 끝나면, 다음','은 외부 함수 호출의 매개 변수 목록을 계속합니다. 매개 변수가 하나만있는 경우 람다 식 매개 변수 목록에서 괄호를 사용할 필요가 없습니다 (예 :'Test (s => ..., "foo")'). –