C# 每天温习一点(IEnumerable<TSource>)
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>)的更多相关文章
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
- 对Dapper的一点改造
微软推出的ORM, EF在我开发的项目中给我的感觉一直都是慢.优点是高度封装的底层.便于开发. Dapper在多篇性能比较的网站中.都是名列前三.缺点是手写SQL,不便于开发. 如果能结合EF的优 ...
- 【C#夯实】我与接口二三事:IEnumerable、IQueryable 与 LINQ
序 学生时期,有过小组作业,当时分工一人做那么两三个页面,然而在前端差不多的时候,我和另一个同学发生了争执.当时用的是简单的三层架构(DLL.BLL.UI),我个人觉得各写各的吧,到时候合并,而他觉得 ...
- IEnumerable和IEnumerable<T>接口
IEnumerable和IEnumerable<T>接口 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach ...
- IEnumerable<T>与IQueryable<T>以及.net的扩展方法
首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...
- IEnumerable接口的Aggregate方法
以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码: string result ="": foreach (var item in items) { result+=i ...
- Linq/List/Array/IEnumerable等集合操作
来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1 LINQ查询结果集 1 2 Sy ...
- 轻轻的扩展了一下IEnumerable<T>
今天用EF写东西玩,觉得IEnumerable里面除了where().select(),是不是能添加点其他方法呢. 想做就做,F12到方法定义: public static IEnumerable&l ...
- C# IQueryable和IEnumerable的区别
在使用EF查询数据的时候,我们常用的查询数据方式有linq to sql,linq to object, 查询返回的结果有两种类型:IQueryable.IEnumerable,两者内部的处理机制是完 ...
随机推荐
- ecshop以幻灯版调用首页主广告显示
默认的是index_ad.lbi模板有一个$flash变量了,但在搜索搜索没发现 <!--{foreach from=$flash name=no item=flash}--> <l ...
- c/c++字符串处理大集合
rember this strncpy(a,b,5); a[5]='\0'; char a[10]; memset(a,'#',sizeof(a)); a[10]='\0'; 刚开始学C/C++时,一 ...
- Laravel入门笔记
Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...
- cocos2d-x 让精灵按照自己设定的运动轨迹行动
转自:http://blog.csdn.net/ufolr/article/details/7447773 在cocos2d中,系统提供了CCMove.CCJump.CCBezier(贝塞尔曲线)等让 ...
- PostgreSQL的schema信息,存储于何处
查看schema信息: [pgsql@localhost bin]$ ./psql psql () Type "help" for help. pgsql=# create sch ...
- 检查dns服务器是否可用
#%windir%\system32\WindowsPowerShell\v1.0\powershell.exe D:\PSScript\ERP_Production_Script\ERPRF_Upd ...
- 【HMTL】3D标签球
这是一个3D TAG 在网站展示中是个不错的东东,能让人眼前一亮,值得收藏. 这个是效果: 源码下载: 点 击 下 载
- Codeforces Gym 100425D D - Toll Road 找规律
D - Toll RoadTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- IOS 小技巧积累
转自:http://blog.csdn.net/mars2639/article/details/7352012 1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在 ...
- string 对象及其操作
标准库类型string 标准库类型string表示可变长的字符序列,使用string类型必须首先包含string头文件.作为标准库的一部分,string定义在命名空间std中.接下来的示例都假定了已包 ...