
Isn't Func
|
1 2 3 4 5 6 7 8 9 10 11 12 13 | class A { static void Main() { Func<int, bool> func = i => i > 100; Predicate<int> pred = i => i > 100; Test<int>(pred, 150); Test<int>(func, 150); // Error } static void Test< T >(Predicate< T > pred, T val) { Console.WriteLine(pred(val) ?"true" :"false"); } } |
更加灵活的
(加上名称
即使没有泛型,您也可以拥有不同的委托类型,这些委托类型的签名和返回类型相同。 例如:
1 2 3 4 5 6 7 8 9 10 11 12 | namespace N { // Represents a method that takes in a string and checks to see // if this string has some predicate (i.e. meets some criteria) // or not. internal delegate bool StringPredicate(string stringToTest); // Represents a method that takes in a string representing a // yes/no or true/false value and returns the boolean value which // corresponds to this string internal delegate bool BooleanParser(string stringToConvert); } |
在上面的示例中,两个非泛型类型具有相同的签名和返回类型。 (实际上也与
这有点像如果我创建两个类,分别是