lambda表达式与委托与线程初步谈论-基于刘铁锰视频观后操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;//线程
using System.Threading;//引用线程方法

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
         //委托详解
            //Func返回带参数类型的委托方法且当方法含有多个参数时需要调用泛型<参数1,参数2……等等,最后加上返回值参数>
            //Action返回不带参数类型的委托方法且当方法含有多个参数时与一样Func一样需要调用泛型<参数1,参数2……等等>
            //var func = new Func<int,int,int>(Add);
            //int sc = func(3,4);
            //Console.WriteLine("{0}++", sc);
            //var action = new Action<double,int>(Getround);
            //Console.WriteLine("{0}",action);
            //action.Invoke(3.14,5);                        
            //ProductFactory productFactory = new ProductFactory();
            //WrapFactory warpFact = new WrapFactory();
            ////定义委托去获得产品的名称
            //Func<Product> Product1 = new Func<Product>(productFactory.Father);//返回Product的类型的产品名称
            //Func<Product> Product2 = new Func<Product>(productFactory.Mother);//模板方法            
            //logger newlog=new logger();
            ////action委托将事件记录器当做参数去记录
            //Action<Product> log = new Action<Product>(newlog.Logger);//回调方法
            ////将委托得到名称产品复制到box的产品信息中
            //Box box1 = warpFact.WrapProduct(Product1,log);//工厂包装以这个产品名称的产品两个参数
            //Box box2 = warpFact.WrapProduct(Product2,log);
            //Console.WriteLine(box1.Product.Name);
            //Console.WriteLine(box2.Product.Name);
        //lambda表达式应用
            //(x, y) => { return x + y; }lambda表达式
            //Dosomething<int>((int x,int y)=>{return x+y;},3,4);//完整版本
            //Dosomething((x, y) => { return x + y; }, 3, 4);//简化版本因为泛型委托当中含有类型推断所以直接省略了一部分int

        //多线程:
            //Student stu1 = new Student() { ID = 1,Pencolor=ConsoleColor.Yellow};
            //Student stu2 = new Student() { ID = 2, Pencolor = ConsoleColor.Green };
            //Student stu3 = new Student() { ID = 3, Pencolor = ConsoleColor.Red };
            //stu1.DoHomework();
            //stu2.DoHomework();
            //stu3.DoHomework(); //直接同步调用
            
            //Action action1 = new Action(stu1.DoHomework);
            //Action action2 = new Action(stu2.DoHomework);
            //Action action3 = new Action(stu3.DoHomework);//定义委托

            //action1.Invoke();
            //action2.Invoke();
            //action3.Invoke();//间接同步调用

            //action1+=action2;
            //action1+=action3;
            //action1.Invoke();//多播委托-间接同步调用

            //Thread thread1 = new Thread(new ThreadStart(stu1.DoHomework));
            //Thread thread2 = new Thread(new ThreadStart(stu2.DoHomework));
            //Thread thread3 = new Thread(new ThreadStart(stu3.DoHomework));
            //thread1.Start();
            //thread2.Start();
            //thread3.Start();//显式异步调用

            //Task task1 = new Task(new Action(stu1.DoHomework));
            //Task task2 = new Task(new Action(stu2.DoHomework));
            //Task task3 = new Task(new Action(stu3.DoHomework));
            //task1.Start();
            //task2.Start();
            //task3.Start();//显式异步委托调用

            //action1.BeginInvoke(null,null);
            //action2.BeginInvoke(null,null);
            //action3.BeginInvoke(null,null);//隐式异步委托调用
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.ForegroundColor = ConsoleColor.Cyan;
            //    Console.WriteLine("Main thread:{0}", i);
            //    Thread.Sleep(500);
            //}
        }
        //static int Add(int x, int y) {
        //    int s = 0;
        //    s = x + y;
        //    Console.WriteLine("{0}",s);
        //    return s;
        //}
        //static void Getround(double x,int y) {
        //    for (int i = 0; i < y; i++) 
        //    {
        //        Console.WriteLine("{0}",x);
        //    }
        //}        
        static void Dosomething<T>(Func<T,T,T> func,T x,T y) 
        {
            T res=func(x,y);
            Console.WriteLine(res);
        }
    }
    class Student 
    {
        //public int ID { get; set; }//字段声明构造器
        //public ConsoleColor Pencolor { get; set; }
        //public void DoHomework() 
        //{
        //    for(int i=1;i<=5;i++)
        //    {
        //        Console.ForegroundColor = this.Pencolor;
        //        Console.WriteLine("学生ID:{0},做了{1}小时作业",this.ID,i);
        //        Thread.Sleep(500);
        //    }
        //}
    }
    //class logger
    //    //记录事件触发的情况
    //{
    //    public void Logger(Product prot) 
    //    {
    //        Console.WriteLine("产平名称:{0},产品价格:{1},生产日期:{2}",prot.Name,prot.Price,DateTime.UtcNow);
    //    }
    //}
    //class Box
    //{
    //    //包装产品信息 字段
    //    public Product Product
    //    {
    //        get;
    //        set;
    //    }
    //}
    //class Product
    //{
    //    //生产产品名称 字段
    //    public string Name
    //    {
    //        get;
    //        set;
    //    }
    //    public double Price
    //    {
    //        get;
    //        set;
    //    }
    //}
    //class WrapFactory
    //{
    //    //包装工厂
    //    //实际就是一个方法
    //    public Box WrapProduct(Func<Product> getProduct, Action<Product> log)//定义了一个产品类型的委托
    //    {
    //        Box box = new Box();
    //        Product Product = getProduct.Invoke();//将产品从委托的invoke方法当中获得
    //        if (Product.Price >= 50)
    //        {                
    //            log(Product);
    //        }
    //        box.Product = Product;//将产品赋值为box的产品信息使得box有了产品
    //        return box;//返回该产品
    //    }
    //}
    //class ProductFactory 
    //{
    //    //需要生产的产品1
    //    public Product Mother()
    //    {
    //        Product Prot = new Product();
    //        Prot.Name = "士力架";
    //        Prot.Price = 78;
    //        return Prot;
    //    }
    //    //需要生产的产品2
    //    public Product Father()
    //    {
    //        Product Prot = new Product();
    //        Prot.Name = "牙膏";
    //        Prot.Price = 12.5;
    //        return Prot;
    //    }
    //}
}

请复制代码后操作,谢谢,此处推荐刘铁锰老师的视频 https://www.bilibili.com/video/av7606481/?p=23 希望能和大家一起进步

原文地址:https://www.cnblogs.com/sandaman2019/p/10420654.html