博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链式编程学习之练习篇
阅读量:4703 次
发布时间:2019-06-10

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

C#之链式编程(函数模式编程),最近很好像很火,我也尝试练习下。

学习链式编程 需要懂两个知识点:

1.必须会扩展方法

2.lambda+(Func,Action)

好了,下面是我的学习代码:

class Program    {        // study chained call 链式调用        static void Main(string[] args)        {            //One             //Product p = new Product() { ProductName = "iphone5", Price = 300 }.First().Sencond().Third();                        //Two            List
list = new List
() { new Product{ProductName="iphone5",Price=300}, new Product{ProductName="iphone4s",Price=200}, new Product{ProductName="ipad4",Price=100} }; var s = list.GetIphone
(p => { if (p.ProductName.Contains("iphone")) { return p; } else return null; }).getFirst
(); Console.WriteLine(s.Price); Console.ReadKey(); } } public class Product { public string ProductName { get; set; } public int Price { get; set; } public Product First() { Console.WriteLine("First"); return this; } public Product Sencond() { Console.WriteLine("Second"); return this; } public Product Third() { Console.WriteLine("Third"); return this; } } static class ProductExtension { //get iphone public static IEnumerable
GetIphone
(this IEnumerable
source, Func
selector) { foreach (var t in source) { if (selector(t) != null) { yield return selector(t); } } } //get first public static Tsource getFirst
(this IEnumerable
ilist) { return ilist.First(); } }

 

转载于:https://www.cnblogs.com/flyfish2012/p/3250032.html

你可能感兴趣的文章
python之hasattr、getattr和setattr函数
查看>>
maven使用阿里镜像配置文件
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
P2709 小B的询问
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>