怎么实现ienumerable list接口?

实现接口IEnumerable,掌握foeach的使用,并对双向链表进行简单的操作_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
8页免费4页免费34页免费12页免费9页免费 14页免费3页免费2页免费6页免费7页免费
实现接口IEnumerable,掌握foeach的使用,并对双向链表进行简单的操作|
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大尺寸(630*500pix)
你可能喜欢构建可反转排序的泛型字典类(5)--实现IEnumerable>接口 - abatei - 博客园
Blog Stats
Posts - 51
Stories - 0
Comments - 324
Trackbacks - 10
5. 实现IEnumerable&KeyValuePair&TKey, TValue&&接口
ReversibleSortedList
public&class&ReversibleSortedList&TKey,&TValue&&:
&&&&&&&&IDictionary&TKey,&TValue&,&ICollection&KeyValuePair&TKey,&TValue&&,
&&&&&&&&IEnumerable&KeyValuePair&TKey,&TValue&&,&IDictionary,&ICollection,&IEnumerable
6IDictionary&TKey, TValue&IDictionary6IDictionary&TKey, TValue&ICollection&KeyValuePair&TKey, TValue&&IEnumerable&KeyValuePair&TKey, TValue&&IDictionaryICollectionIEnumerable
2 &ReversibleSortedList
ReversibleSortedList1300ICollectionIEnumerableIEnumerableIEnumerableIDictionary&TKey, TValue&IDictionaryIEnumerator&KeyValuePair&K, V&&IDictionaryEnumeratorIDictionary&TKey, TValue&IDictionary3 &Enumerator&K, V&
ReversibleSortedListversioninsertversionMSDNMSDNDictionary&TKey, TValue&ReversibleSortedList
Dictionary&(Of &(TKey, TValue&)&)
versionReversibleSortedList
IDictionarySimpleDictionaryEnumerator
public&SimpleDictionaryEnumerator(SimpleDictionary&sd)
&&&&&&&&&&&&items&=&new&DictionaryEntry[sd.Count];
&&&&&&&&&&&&Array.Copy(sd.items,&<span style="color: #,&items,&<span style="color: #,&sd.Count);
ReversibleSortedListversion
ReversibleSortedList 0.3version0.4
private&struct&Enumerator&K,&V&&:&IEnumerator&KeyValuePair&K,&V&&,&IDisposable,
&&&&&&&&IDictionaryEnumerator,&IEnumerator
&&&&&&&&private&ReversibleSortedList&K,&V&&_ReversibleSortedL
&&&&&&&&private&K&
&&&&&&&&private&V&
&&&&&&&&private&int&
&&&&&&&&private&int&
&&&&&&&&internal&Enumerator(ReversibleSortedList&K,&V&&ReversibleSortedList)
&&&&&&&&{&&&//获取外部类中的元素引用,以对它进行枚举
&&&&&&&&&&&&this._ReversibleSortedList&=&ReversibleSortedL
&&&&&&&&&&&&this.index&=&<span style="color: #;
&&&&&&&&&&&&this.version&=&this._ReversibleSortedList.//记录外部类版本号
&&&&&&&&&&&&//设置键和值为其默认类型,请参考:
&&&&&&&&&&&&//http://cgbluesky./blog/static//
&&&&&&&&&&&&this.key&=&default(K);&
&&&&&&&&&&&&this.value&=&default(V);
&&&&&&&&public&void&Dispose()
&&&&&&&&&&&&this.index&=&<span style="color: #;
&&&&&&&&&&&&this.key&=&default(K);
&&&&&&&&&&&&this.value&=&default(V);
&&&&&&&&object&IDictionaryEnumerator.Key
&&&&&&&&&&&&get
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if&((this.index&==&<span style="color: #)&||
&&&&&&&&&&&&&&&&&&&&(this.index&==&(this._ReversibleSortedList.Count&+&<span style="color: #)))
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&throw&new&InvalidOperationException(
&&&&&&&&&&&&&&&&&&&&&&&&&&&"不能进行枚举操作.");
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&return&this.
&&&&&&&&&&&&}
&&&&&&&&public&bool&MoveNext()
&&&&&&&&&&&&if&(this.version&!=&this._ReversibleSortedList.version)
&&&&&&&&&&&&{&&&//版本检查
&&&&&&&&&&&&&&&&throw&new&InvalidOperationException("枚举版本检查失败!");
&&&&&&&&&&&&}
&&&&&&&&&&&&if&(this.index&&&this._ReversibleSortedList.Count)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&this.key&=&this._ReversibleSortedList.keys[this.index];
&&&&&&&&&&&&&&&&this.value&=&this._ReversibleSortedList.values[this.index];
&&&&&&&&&&&&&&&&this.index++;
&&&&&&&&&&&&&&&&return&true;
&&&&&&&&&&&&}
&&&&&&&&&&&&this.index&=&this._ReversibleSortedList.Count&+&<span style="color: #;
&&&&&&&&&&&&this.key&=&default(K);
&&&&&&&&&&&&this.value&=&default(V);
&&&&&&&&&&&&return&false;
&&&&&&&&DictionaryEntry&IDictionaryEnumerator.Entry
&&&&&&&&&&&&get
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if&((this.index&==&<span style="color: #)&||
&&&&&&&&&&&&&&&&&&&&(this.index&==&(this._ReversibleSortedList.Count&+&<span style="color: #)))
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&throw&new&InvalidOperationException("不能进行枚举操作.");
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&return&new&DictionaryEntry(this.key,&this.value);
&&&&&&&&&&&&}
&&&&&&&&public&KeyValuePair&K,&V&&Current
&&&&&&&&&&&&get
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&return&new&KeyValuePair&K,&V&(this.key,&this.value);
&&&&&&&&&&&&}
&&&&&&&&object&IEnumerator.Current
&&&&&&&&&&&&get
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if&((this.index&==&<span style="color: #)&||
&&&&&&&&&&&&&&&&&&&&(this.index&==&(this._ReversibleSortedList.Count&+&<span style="color: #)))
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&throw&new&InvalidOperationException("不能进行枚举操作");
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&return&new&DictionaryEntry(this.key,&this.value);
&&&&&&&&&&&&}
&&&&&&&&object&IDictionaryEnumerator.Value
&&&&&&&&&&&&get
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if&((this.index&==&<span style="color: #)&||
&&&&&&&&&&&&&&&&&&&&(this.index&==&(this._ReversibleSortedList.Count&+&<span style="color: #)))
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&throw&new&InvalidOperationException("不能进行枚举操作");
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&return&this.
&&&&&&&&&&&&}
&&&&&&&&void&IEnumerator.Reset()
&&&&&&&&&&&&if&(this.version&!=&this._ReversibleSortedList.version)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&throw&new&InvalidOperationException("枚举版本检查失败!");
&&&&&&&&&&&&}
&&&&&&&&&&&&this.index&=&<span style="color: #;
&&&&&&&&&&&&this.key&=&default(K);
&&&&&&&&&&&&this.value&=&default(V);
3MSDNIEnumerable&KeyValuePair&TKey, TValue&& IEnumerable
public&class&ReversibleSortedList&TKey,&TValue&&:
&&&&IEnumerable&KeyValuePair&TKey,&TValue&&,&IEnumerable
GetEnumerator()ReversibleSortedList
&IEnumerator&KeyValuePair&TKey,&TValue&&
&&&&&&&&&IEnumerable&KeyValuePair&TKey,&TValue&&.GetEnumerator()
&&&&&&&&return&new&Enumerator&TKey,&TValue&(this);
&&&&IEnumerator&IEnumerable.GetEnumerator()
&&&&&&&&return&new&Enumerator&TKey,&TValue&(this);
static&void&Main()
&&&&&&&&ReversibleSortedList&int,&string&&rs=new&ReversibleSortedList&int,&string&();
&&&&&&&&rs.Add(<span style="color: #,"a");
&&&&&&&&rs.Add(<span style="color: #,"b");
&&&&&&&&rs.Add(<span style="color: #,"c");
&&&&&&&&rs.Add(<span style="color: #,"d");
&&&&&&&&rs.Add(<span style="color: #,"e");
&&&&&&&&rs.Add(<span style="color: #,"f");
&&&&&&&&foreach&(KeyValuePair&int,&string&&d&in&rs)
&&&&&&&&&&&&Console.WriteLine(d.Key&+&"&&&&"&+&d.Value);
ReversibleSortedList 0.4
IDictionaryEnumeratorforeachDictionaryEntryIDictionaryGetEnumerator()IDictionaryEnumeratorIDictionaryDictionaryEntryIDictionary温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
阅读(1091)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
id:'fks_080069',
blogTitle:'C#接口2',
blogAbstract:'1.5 &玩转接口
本节将介绍以下内容:
— 什么是接口
— 接口映射本质
— 面向接口编程
— 典型的.NET接口
1.5.1& 引言
接口,是面向对象设计中的重要元素,也是打开设计模式精要之门的钥匙。玩转接口,就意味着紧握这把钥匙,打开面向对象的抽象之门,成全设计原则、成就设计模式,实现集优雅和灵活于一身的代码艺术。
本节,从接口由来讲起,通过概念阐述、面向接口编程的分析以及.NET框架中的典型接口实例,勾画一个理解接口的框架蓝图,通过这一蓝图将会了解玩转接口的学习曲线。
1.5.2& 什么是接口
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:5,
publishTime:8,
permalink:'blog/static/',
commentCount:1,
mainCommentCount:1,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}对EF开发来说,导航属性肯定都用过,事实上,它是由VS IDE工具根据你的数据库关系结构自动生成的外键属性,在类视图中可以看到相关属性,它是以外键表名来标识的,如果是一对多的关系,那么,它会为属性加上ICollection泛型集合用来标识,而今天我们要说的当然不是自动生成的,而是手动加的属性,这样属性需要我们手动进行join,然后把它按需赋值,而使用include当然是无效的,呵呵。
一般地,我们习惯上把集合属性定义为List&T&,但是,对于linq to entities来说,这个东西并不是很受欢迎,而标准结果集大家都知道是IEnumerable&T&,它是所有集合的基类,自身只提供了集合遍历的方法,这也是我们不用它作为导航属性的原因,因为我们一般需要为导航集合赋值的,而使用IEnumerable赋值就比较麻烦,需要借助List等集合。
引入IEnumerable的原因主要是List无法实现一个复杂的查询,如图:
这个查询返回一个派生类型,下面的复杂查询将会用到上面的结果,而这时,List类型的导航属性将是不被允许的
我们通过监视器可以看到,查询返回的默认是IEnumerable,所以,我们要属性改为IEnumerable,结果当然是正常的
而我们之前不用IEnumerable的原因,就是因为它的方法太单调了,没有添加,移除,查找等方法,而这时,不用它又不行,所以,只能把它进行扩展了,呵呵
扩展方法如下:
/// &summary&
/// IEnumerable接口的扩展方法,支持它的实现类是List的情况
/// &/summary&
public static class IEnumerableExtensions
/// &summary&
/// 向集合中添加元素
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="collection"&&/param&
/// &param name="value"&&/param&
public static void Add&T&(this IEnumerable&T& collection, T value)
(collection as List&T&).Add(value);
/// &summary&
/// 从集合中删除元素
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="collection"&&/param&
/// &param name="value"&&/param&
public static void Remove&T&(this IEnumerable&T& collection, T value)
(collection as List&T&).Remove(value);
/// &summary&
/// 检索集合中是否包含某个元素
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="collection"&&/param&
/// &param name="value"&&/param&
/// &returns&&/returns&
public static bool Contains&T&(this IEnumerable&T& collection, T value)
return (collection as List&T&).Contains(value);
调用也是十分简单,和List类型的一样样,呵呵,舒服!
阅读(...) 评论()

我要回帖

更多关于 ienumerable list 的文章

 

随机推荐