IEnumerable扩展
void Main()
{
// This uses a custom 'Pair' extension method, defined below. Customers
.Select (c => c.Name.ToUpper())
.Pair() // Local from this point on.
.OrderBy (n => n)
.Dump(); // Here's a more substantial example: Customers
.Select (c => c.Name.ToUpper())
.OrderBy (n => n)
.Pair() // Local from this p.oint on.
.Select ((n, i) => "Pair " + i.ToString() + " = " + n)
.Dump(); Customers.Select (c => c.Name.ToUpper()).Add().Dump();
} public static class MyExtensions
{
public static IEnumerable<string> Pair (this IEnumerable<string> source)
{
string firstHalf = null;
foreach (string element in source)
if (firstHalf == null)
firstHalf = element;
else
{
yield return firstHalf + ", " + element;
firstHalf = null;
}
} public static IEnumerable<string> Add (this IEnumerable<string> source)
{
int index=;
foreach (string element in source)
{
yield return "NO."+index+++" " + element;
} }
}
IEnumerable扩展的更多相关文章
- 为IEnumerable扩展一个ForEach方法
IEnumerable没有一个ForEach方法,我们可以使用C#写一个扩展方法: Source Code: using System; using System.Collections.Generi ...
- C# DataTable,DataSet,IList,IEnumerable 互转扩展属性
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- 【细语】C#之扩展方法原理及其使用
1.写在前面 今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后(这个问题会在文章结尾回答).所以写了这边文章,力图从原理角度解释扩展方法及其使用. 以下为主要内容 ...
- 状态机/迭代器/LINQ/协程
状态机 有限状态机(Finite State Machine 或 Finite State Automata)是软件领域中一种重要的工具. 状态机允许一个对象在其内部状态改变时改变它的行为.对象内部状 ...
- .net 温故知新:【6】Linq是什么
1.什么是Linq 关于什么是Linq 我们先看看这段代码. List<int> list = new List<int> { 1, 1, 2, 2, 3, 3, 3, 5, ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
- IEnumerable<T>与IQueryable<T>以及.net的扩展方法
首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...
- 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
假如我有两个集合: public class Teacher { public int Id { get; set; } public string Name { get; set; } } publ ...
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
随机推荐
- python Parent.__init()和super(Child, self)的区别
super函数做的事情 def supper(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cls) + 1] inst生成父 ...
- unity 实时间接光照 解决方案
https://www.youtube.com/watch?v=D7LjsabD4V4 这个很强 他runtime bake lightprobe 之后走assetbundle加载 Place Pro ...
- Android跟踪NDK崩溃信息
1.NDK编译jni时增加调试信息: $NDK_ROOT/ndk-build -B NDK_DEBUG=1 2.发生崩溃时,logcat中收集日志: - ::): Added shared lib / ...
- Spring整合JMS——三种connectionFactory
1.SingleConnectionFactory:对于建立JMS服务器链接的请求会一直返回同一个链接,并且会忽略Connection的close方法调用.(org.springframework.j ...
- [ES6] 11. String Templates
ECMAscript 6 lets us use string templates to gain a lot more control over strings in JavaScript. var ...
- web 表单,脚本验证
1.不能含有中文 var obj = document.form1.txtName.value; if(/.*[\u4e00-\u9fa5]+.*$/.test(obj)) { alert(" ...
- SDL 学习及相关API
SDL_PeepEvents() 在事件队列中搜索特定类型的事件. int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventacti ...
- EAS常用工具类
package com.kingdee.eas.custom; import java.io.File; import java.io.FileNotFoundException; import ja ...
- 学会Git玩转Github笔记(一)——Github基本概念 & 仓库管理
一.github基本概念 使用目的:借助GitHub托管项目代码 1.仓库(Respository) 2.收藏(Star) 3.复制克隆项目(Fork) :分叉—你开源了一个项目,别人想在你这个项目基 ...
- newWindow 弹出的新窗口居中显示
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...