LinQ Lambda表达式用作泛型活动

 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace LambdaExpressionAction
{
    class Program
    {
        static void Main(string[] args)
        {
            //Action:泛型委托,可以将行为捕获为一个可调用的对象
            //只需要给Action提供参数类型,并赋予其一个Lambda表达式即可

            //定义一个输出字符串的委托,s是输出的字符串,TextWriter是一种输出的实例,如Console
            Action<string, TextWriter> 输出委托 = (s, writer) => writer.WriteLine(s);
            //将"Console"字符串在控制台输出
            输出委托("控制台显示输出的字符串!", Console.Out);
            //定义一个输出流,将字符串写入到"c:\temp\text.txt"文本文件的最后一行
            StreamWriter stream = File.AppendText("c:\temp\text.txt");
            //如果用户期望您所写入的设备能够立即反馈,则将 AutoFlush 设置为 true
            stream.AutoFlush = true;
            //调用输出委托
            输出委托("System.IO中显示输出的字符串!", stream);
            Console.ReadLine();
        }
    }
}
原文地址:https://www.cnblogs.com/swtool/p/3840734.html