博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【winfrom】委托Lambda
阅读量:4344 次
发布时间:2019-06-07

本文共 3470 字,大约阅读时间需要 11 分钟。

delegate void Cal();    public partial class Form1 : Form    {        //委托         //2.0之前  命名方法        //2.0引用  匿名方法        //3.0 Lambda  简化匿名方法         //  委托   匿名方法 Lambda        // 委托 是一个类,定义方法的类型,将一个方法当作另一个方法的参数来进行传递        // 类 引用类型        event Caluate CalEvent;         delegate int Caluate(int a, int b);//定义一个委托        public Form1()        {            InitializeComponent();        }        int sum = 4;              //加        private int Add(int x, int y)        {            return sum += x + y;        }        //减        private int Subtract(int x, int y)        {            return sum += x - y;        }        //乘        private int Multiply(int x, int y)        {            return sum += x * y;        }        private void Form1_Load(object sender, EventArgs e)        {            Caluate cal = new Caluate(Add);//委托的实例化            //Caluate cal1 = Subtract;//赋值委托            cal += Subtract;            cal += Multiply;    //多播委托,执行是按添加的顺序来执行            cal -= Subtract;//方法的移除            int re = cal.Invoke(3, 5);            int re1 = cal(3, 5);            //匿名方法            Caluate cal2 = delegate(int x, int y)            {                return x + y;            };            cal2 += delegate(int m, int n)            {                return m * n;            };            int re3 = cal2.Invoke(4, 9);//36                                            //goes to            Caluate cal3 = (int x, int y) => { return x + y; };//Lambda表达式            Caluate cal4 = (x,y) =>  x + y; //Lambda表达式 简化            Cal c = () => { Console.WriteLine("aaaa"); };           //Action 不提供返回值,参数0,1,2.。。。16个参数            Action act = () => { Console.WriteLine("Hello,Welcome!"); };            Action
act1 = s => { Console.WriteLine("Hello,{0}", s); }; Action
act2 = (s,t) => { Console.WriteLine("Hello,{0},{1}", s,t); }; //Func 只提供一个返回值,参数0,1,2.。。。16个参数 Func
func1=()=>"ssss"; Func
func2 = s => s+"ssss"; Func
func3 = (s, i) => { Console.WriteLine("sssssddddd"); return s + "ssss" + i; }; List
list = new List
(); list.Add(new User() { Id = 1, UserName = "小红", Age = 22 }); list.Add(new User() { Id = 1, UserName = "路人", Age = 20}); list.Add(new User() { Id = 1, UserName = "Jonathan", Age = 24 }); list.Add(new User() { Id = 1, UserName = "Telnet", Age = 26 }); list.Add(new User() { Id = 1, UserName = "陈小哥", Age = 21 }); list.Add(new User() { Id = 1, UserName = "anymore", Age = 25 }); list.Add(new User() { Id = 1, UserName = "LTE-ZHE", Age = 23 }); list.Add(new User() { Id = 1, UserName = "Antidote", Age = 27 }); list.Add(new User() { Id = 1, UserName = "Jerry", Age = 30 }); list.Add(new User() { Id = 1, UserName = "Lily", Age = 29 }); //扩展方法 定义在静态类中的静态方法 string ss = "234"; int iSS = ss.ToInt(); list.Where(u => u.Age > 24); list.OrderBy(u => u.Id); list.OrderByDescending(u => u.Id); }

  

public class User    {        public int Id { get; set; }        public string UserName { get; set; }        public int Age { get; set; }    }

  

public static class Converter    {        public static int ToInt(this string str)        {            int reInt = 0;            int.TryParse(str, out reInt);            return reInt;        }    }

  

转载于:https://www.cnblogs.com/Microera/p/8648619.html

你可能感兴趣的文章
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
IOS内存管理
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>