Func 제네릭 유형은 익명 메소드를 저장하며 매개변수화된 유형입니다.
아래 예에는 4개의 func 유형 인스턴스가 있습니다 -
첫 번째 유형은 int를 수신하고 문자열을 반환합니다.
Func<int, string> one = (p) => string.Format("{0}", p); 두 번째 유형은 bool 및 long을 수신하고 문자열을 반환합니다.
Func<bool, long, string> two = (q, p) =>string.Format("{0} and {1}", q, p); 세 번째 유형은 bool 및 int를 수신하고 문자열을 반환합니다.
Func<bool, int, string> three = (q, p) => string.Format("{0} and {1}", q, p); 네 번째 유형은 10진수를 받고 문자열을 반환합니다.
Func<decimal, string> four = (p) =>string.Format("{0}", p); 표시하는 방법을 알아보겠습니다 -
예시
using System;
using System.IO;
namespace Demo {
class Program {
static void Main(string[] args) {
// four func type instance
// first type receives int and returns string
Func<int, string> one = (p) =>
string.Format("{0}", p);
// second type receives bool & long and returns string
Func<bool, long, string> two = (q, p) =>
string.Format("{0} and {1}", q, p);
// three type receives bool & int and returns string
Func<bool, int, string> three = (q, p) =>
string.Format("{0} and {1}", q, p);
// fourth type receives decimal and returns string
Func<decimal, string> four = (p) =>
string.Format("{0}", p);
Console.WriteLine(one.Invoke(25));
Console.WriteLine(two.Invoke(false, 76756566));
Console.WriteLine(three.Invoke(true, 50));
Console.WriteLine(four.Invoke(1.2m));
}
}
} 출력
25 False and 76756566 True and 50 1.2