Func<T,TResult> Delegate
封装具有一个参数的方法,并返回由TResult参数指定的类型的值。
// Declare a Func variable and assign a lambda expression to the
// variable. The method takes a string and converts it to uppercase.
Func<string, string> selector = str => str.ToUpper();
// Create an array of strings.
string[] words = { "orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable<String> aWords = words.Select(selector);
// Output the results to the console.
foreach (String word in aWords)
Console.WriteLine(word);
using System;
delegate string ConvertMethod(string inString);
public class DelegateExample
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMeth(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
// This code example produces the following output:
//
// DAKOTA
Func<string, string> convert = delegate(string s)
{ return s.ToUpper();};
string name = "Dakota";
Console.WriteLine(convert(name));
// This code example produces the following output:
//
// DAKOTA
Func<string, string> convert = s => s.ToUpper();
string name = "Dakota";
Console.WriteLine(convert(name));
// This code example produces the following output:
//
// DAKOTA
Func<T1,T2,TResult> Delegate
using System;
using System.Collections.Generic;
using System.Linq;
public class Func3Example
{
public static void Main()
{
Func<String, int, bool> predicate = (str, index) => str.Length == index;
String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);
foreach (String word in aWords)
Console.WriteLine(word);
}
}
using System;
delegate string[] ExtractMethod(string stringToManipulate, int maximum);
public class DelegateExample
{
public static void Main()
{
// Instantiate delegate to reference ExtractWords method
ExtractMethod extractMeth = ExtractWords;
string title = "The Scarlet Letter";
// Use delegate instance to call ExtractWords method and display result
foreach (string word in extractMeth(title, 5))
Console.WriteLine(word);
}
private static string[] ExtractWords(string phrase, int limit)
{
char[] delimiters = new char[] {' '};
if (limit > 0)
return phrase.Split(delimiters, limit);
else
return phrase.Split(delimiters);
}
}
using System;
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference ExtractWords method
Func<string, int, string[]> extractMethod = ExtractWords;
string title = "The Scarlet Letter";
// Use delegate instance to call ExtractWords method and display result
foreach (string word in extractMethod(title, 5))
Console.WriteLine(word);
}
private static string[] ExtractWords(string phrase, int limit)
{
char[] delimiters = new char[] {' '};
if (limit > 0)
return phrase.Split(delimiters, limit);
else
return phrase.Split(delimiters);
}
}
using System;
public class Anonymous
{
public static void Main()
{
Func<string, int, string[]> extractMeth = delegate(string s, int i)
{ char[] delimiters = new char[] {' '};
return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
};
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extractMeth(title, 5))
Console.WriteLine(word);
}
}
using System;
public class LambdaExpression
{
public static void Main()
{
char[] separators = new char[] {' '};
Func<string, int, string[]> extract = (s, i) =>
i > 0 ? s.Split(separators, i) : s.Split(separators) ;
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extract(title, 5))
Console.WriteLine(word);
}
}