using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace LocalApp.ConsoleApp
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delegate void LambdaHandler();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Func 封装一个具有 1 - 4 个参数并返回 TResult 参数指定的类型值的方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Func<int, string> func = delegate(int i)
{ return Convert.ToString(i * i); };
Console.WriteLine(func(3));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// lambda 表达式,i 参数
Func<int, string> func2 = i => Convert.ToString(i * i);
Console.WriteLine(func2(4));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Func<string, string> func3 = a => a.ToUpper();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] array =
{ "hebei","hubei","beijing","12" };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IEnumerable<string> _array = array.Where<string>(b => b.EndsWith("i")); // array.Select(func3);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string i in _array)
Console.WriteLine(i);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Func<string, string, string, string, string> __func = (a, b, c, d) =>
{ return a + "_" + b + "_" + c + "_" + d; };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(__func("h","e","l","lo"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Func<string> __func2 = () => "123456";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(__func2());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LambdaHandler lam = () => Console.WriteLine( "1111111111");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lam += () => Console.WriteLine("22222222222");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lam();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
//扩展方法
string extTest = "hello world";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(extTest.WordCount());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach(string i in extTest.WordSplit())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(i);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//******************************************/
Console.ReadKey(true);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 扩展方法
/// </summary>
public static class Extensions
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static int WordCount(this String str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return str.Split(new char[]
{ ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static string[] WordSplit(this String str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return str.Split(new char[]
{' ','.','?' }, StringSplitOptions.None);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2009-01-19 17:27
M'
阅读(
574)
评论()
编辑
收藏
举报