1, IEnumerable<TSource> 多数屌丝写成  IEnumerable<T>  无论TSource还是T都代表一个意思:要枚举的对象的类型 。IEnumerable<T>是一个枚举器,该枚举器支持在指定类型的集合上进行简单迭代(官方解释),简单的说就是实现了IEnumerable<T> 接口才能使用foreach 迭代。

2,首先先看下如何实现自定义类的迭代。下面来看一段代码。这里首先定义了一个Dog类,为该类添加了索引器和实现IEnumerable接口。实现的三大方法便可以foreach啦。通常我们在做类似自定义Session管理的时候会采用这样设计思想。

  public class Dogs : IEnumerable, IEnumerator
{
public string name { get; set; }
public bool sex { get; set; }
public List<object> list = new List<object>();
int dex = -;
/// <summary>
/// 屌丝。这是一个索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public object this[int index]
{
get
{
return list[index];
}
set
{
list[index] = value;
}
}
public void Add<T>(T t)
{
list.Add(t);
}
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public object Current
{
get { return this[dex]; }
}
public bool MoveNext()
{
  if (dex >= list.Count-1)
            {
                return false;
            }
dex++;
return true;
}
public void Reset()
{
dex = -;
}
}

这个关于Dogs的类有个索引器。所以它能存储数据。它实现了IEnumerable so,他就能迭代了,是不是很神。

           string[] str = { "", "", "" };
Dogs dogs = new Dogs();
foreach (var item in str)
{
dogs.Add(new Dogs()
{
name = item,
});
}
foreach (Dogs item in dogs)
{
Console.WriteLine(item.name);
}

二:接下来是一个奇奇怪怪的拓展方法:这个方法在字符串处理的时候的很好用,这个可以将一个实现了IEnumerable接口的类如 List<t>拼成字符串。是做前后端交互的必备。可以节省各位屌丝的大量时间。

       public static string ToSealString(this IEnumerable l, string split)
{
split = ",";
StringBuilder strText = new StringBuilder();
var e = l.GetEnumerator();
while (e.MoveNext())
{ strText.Append(e.Current.ToString() + split);
}
if (strText.Length > )
{
strText = strText.Remove(strText.Length - split.Length, );
}
return strText.ToString();
}

ps:欢迎指正。补充,下班了,以后再补充。

C# 每天温习一点(IEnumerable<TSource>)的更多相关文章

  1. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  2. 对Dapper的一点改造

    微软推出的ORM, EF在我开发的项目中给我的感觉一直都是慢.优点是高度封装的底层.便于开发. Dapper在多篇性能比较的网站中.都是名列前三.缺点是手写SQL,不便于开发.   如果能结合EF的优 ...

  3. 【C#夯实】我与接口二三事:IEnumerable、IQueryable 与 LINQ

    序 学生时期,有过小组作业,当时分工一人做那么两三个页面,然而在前端差不多的时候,我和另一个同学发生了争执.当时用的是简单的三层架构(DLL.BLL.UI),我个人觉得各写各的吧,到时候合并,而他觉得 ...

  4. IEnumerable和IEnumerable<T>接口

    IEnumerable和IEnumerable<T>接口 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach ...

  5. IEnumerable<T>与IQueryable<T>以及.net的扩展方法

    首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...

  6. IEnumerable接口的Aggregate方法

    以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码: string result ="": foreach (var item in items) { result+=i ...

  7. Linq/List/Array/IEnumerable等集合操作

    来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1    LINQ查询结果集    1 2    Sy ...

  8. 轻轻的扩展了一下IEnumerable<T>

    今天用EF写东西玩,觉得IEnumerable里面除了where().select(),是不是能添加点其他方法呢. 想做就做,F12到方法定义: public static IEnumerable&l ...

  9. C# IQueryable和IEnumerable的区别

    在使用EF查询数据的时候,我们常用的查询数据方式有linq to sql,linq to object, 查询返回的结果有两种类型:IQueryable.IEnumerable,两者内部的处理机制是完 ...

随机推荐

  1. RSA前台js加密,后台C#解密

    一.需求: 为了安全,项目中前台登陆用的密码需要加密传到后台,后台c#解密登陆密码. 二.解决方案 采用非对称加密算法RSA来达到目的,前台登陆页面一加载便发送一次ajax请求获取后台产生的公钥,用于 ...

  2. 深入浅出之Smarty模板引擎工作机制(一)

    深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...

  3. BZOJ 2456: mode 水题

    2456: mode Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php? ...

  4. 跨平台实现wchar_t转成char

    位宽.其实知道了这个以后,要在wchar_t 和 char两种类型之间转换就不难实现了. wchar_t 转换为char 的代码如下: 有如下的wchar_t和char变量 wchar_t w_cn ...

  5. psp开发------汉化插件

    近期略微研究了下psp汉化,写了个汉化插件,在这记录下.聊以慰藉. 传统的汉化流程找码表,字库,破解什么这里不多讲,网上有教程.以下说下一种另类汉化方法.特别对于难以破解字库的游戏,当然这样的方法也有 ...

  6. iOS开发——UI_swift篇&UITableView实现索引功能

    UITableView实现索引功能     关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...

  7. lucene_indexWriter说明、索引库优化

    IndexWriter Hibernate的SessionFactory 在Hibernate中.一般保持一个数据库就仅仅有一个SessionFactory.由于在SessionFactory中维护二 ...

  8. /proc/sys/net/ipv4/下各项的意义

        /proc/sys/net/ipv4/icmp_timeexceed_rate这个在traceroute时导致著名的“Solaris middle star”.这个文件控制发送ICMP Tim ...

  9. PAT 1019

    1019. General Palindromic Number (20) A number that will be the same when it is written forwards or ...

  10. Programming Assignment 3: Collinear Points

    The problem. Given a set of N distinct points in the plane, draw every (maximal) line segment that c ...