Материал из .Net Framework эксперт
Accumulator function
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class MainClass
{
static void Main()
{
Func<int, Func<int, int>> accumulatorGenerator = (n => (i => n += i+8));
var accumulator = accumulatorGenerator(1);
Console.WriteLine(accumulator(1));
}
}
Add extension to Func
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class CurryingExtensions
{
public static Func<A, Func<B, R>> myExt<A, B, R>(this Func<A, B, R> f)
{
return a => b => f(a, b);
}
}
class MainClass
{
static void Main()
{
Func<int, int, int> adder = (x, y) => (x + y);
Func<int, Func<int, int>> c = adder.myExt();
Func<int, int> addsTwo = c(2);
Console.WriteLine(addsTwo(5));
}
}
Build the necessary Func<> delegates using anonymous methods
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string[] currentVideoGames = {"test", "this is a test", "asdf", "game 1","game 2", "game 3"};
Func<string, bool> searchFilter = delegate(string game) { return game.Length > 6; };
Func<string, string> itemToProcess = delegate(string s) { return s; };
var subset = currentVideoGames
.Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess);
// Print out the results.
foreach (var game in subset)
Console.WriteLine("Item: {0}", game);
}
}
Double recursive extension
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class DoubleApplicationExtensions
{
public static Func<T, T> ApplyTwice<T>(this Func<T, T> original)
{
return x => original(original(x));
}
}
class MainClass
{
static void Main()
{
Func<int, int> incrementer = x => x + x;
Func<int, int> doubleIncrementer = incrementer.ApplyTwice();
Console.WriteLine(doubleIncrementer(5));
}
}
Lambda Expression for Func
using System;
using System.Collections.Generic;
using System.Text;
using System.ruponentModel;
class LambdaExpression
{
static void Main()
{
Func<int, int, string> func = (x, y) => (x * y).ToString();
Console.WriteLine(func(5, 20));
}
}
Lambda Expression to retrieve string length
using System;
using System.ruponentModel;
class MainClass
{
static void Main()
{
Func<string, int> returnLength = (string text) => { return text.Length; };
Console.WriteLine(returnLength("Hello"));
}
}
Overloading By Delegate Return Type
using System;
using System.ruponentModel;
class MainClass
{
static void Execute(Func<int> action)
{
Console.WriteLine("action returns an int: " + action());
}
static void Execute(Func<double> action)
{
Console.WriteLine("action returns a double: " + action());
}
static void Main()
{
Execute(() => 1);
}
}
Pass the delegates into the methods of Sequence
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string[] currentVideoGames = {"test", "this is a test",
"asdf", "game 1",
"game 2", "game 3"};
Func<string, bool> searchFilter = delegate(string game) { return game.Length > 6; };
Func<string, string> itemToProcess = delegate(string s) { return s; };
var subset = currentVideoGames
.Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess);
foreach (var game in subset)
Console.WriteLine("Item: {0}", game);
}
}