Func Delegate
- Func Delegate는 Action Delegate는 비슷하지만, 반드시 리턴 값을 가져야 합니다.
- Func<T1,T2..Result>로 파라미터는 필요에 따라 0~16개를 받아들일 수 있고, 1개의 Result 값을 갖는다.
- Func<T1,T2...TResult> 로 가장 마지막 제네릭 인자가 Return 값이 된다.
Action 활용법
public void Run()
{
//기존 메서드 사용
Func<string, string, string> ShowPerson = ShowPersonInfo;
string result = ShowPerson("춘남", "프로그래머");
Console.WriteLine(result);
//무명메서드 사용
Func<string, string, string> ShowPsersonalInfo = delegate (string str, string job)
{
return $"{str} : {job}";
};
result = ShowPsersonalInfo("추남", "디자이너");
Console.WriteLine(result);
//람다식 사용
Func<string, string, string> personalInfo = (str, job) => $"{str} : {job}";
result = personalInfo("쏭", "경찰");
Console.WriteLine(result);
//Func대신 로컬 함수도 많이 사용합니다.
string GetPersionalInfo(string name, string job)
{
return $"{name} : {job}";
}
Console.WriteLine(GetPersionalInfo("연어", "전특허현가수"));
}
private string ShowPersonInfo(string str, string job)
{
return $"{str} : {job}";
}
결과
춘남 : 프로그래머
추남 : 디자이너
쏭 : 경찰
연어 : 전특허현가수