/// <summary>
/// 自定義泛型可迭代類型
/// </summary>
/// <example>
/// This code shows how to build a instance of <see cref="SelfEnumerable"/>:
/// <code>
/// var enumerable = new SelfEnumerable<typeparam name="T">Person</typeparam>5);
/// enumerable.Add(new Person() { Name = "xfh", Age = 27 });
/// </code>
/// </example>
/// <descript>
/// 靈感來自:
/// The foreach statement executes a statement or a block of statements for each element in an instance of the type
/// that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface.
/// The foreach statement is not limited to those types and can be applied to an instance of any type that satisfies the following cond
/// 1. has the public parameterless GetEnumerator method whose return type is either class, struct, or interface type,
/// 2. the return type of the GetEnumerator method has the public Current property and the public parameterless MoveNext metho
/// whose return type is Boolean.
/// 參考了:
/// https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/Span.cs
/// https://docs.microsoft.com/en-us/dotnet/csharp/iterators
/// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in
/// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/yield
/// </descript>
public class SelfEnumerable<T>
{
private readonly int _capacity;
private T[] _innerArray;
private int _addIndex = 0;
public SelfEnumerable(int capacity)
{
_capacity = capacity;
_innerArray = new T[capacity];
}
/// <summary>
/// 可迭代對象中存儲(chǔ)的對象數(shù)
/// </summary>
/// <value>auto-property</value>
public int Count { get; private set; } = 0;
public T this[int index] => _innerArray[index];
/// <summary>
/// 向可迭代對象中添加元素
/// </summary>
/// <remarks>當(dāng)添加的元素?cái)?shù)超過<see cref="_capacity"/>的值時(shí)原押,新添加的元素會(huì)覆蓋之前的值</remarks>
public void Add(T item)
{
if (_addIndex > _capacity - 1)
{
_addIndex = 0;
}
else
{
Count++;
}
_innerArray[_addIndex] = item;
_addIndex++;
}
public SelfEnumerator GetEnumerator() => new SelfEnumerator(this);
public class SelfEnumerator
{
private readonly SelfEnumerable<T> _selfEnumerable;
private int _seekIndex = -1;
internal SelfEnumerator(SelfEnumerable<T> selfEnumerable)
{
_selfEnumerable = selfEnumerable;
}
public T Current => _selfEnumerable[_seekIndex];
public bool MoveNext()
{
_seekIndex++;
if (_seekIndex > _selfEnumerable.Count - 1)
{
return false;
}
return true;
}
}
}
C# 自定義可迭代類型
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門疼蛾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人艺配,你說我怎么就攤上這事察郁。” “怎么了转唉?”我有些...
- 文/不壞的土叔 我叫張陵皮钠,是天一觀的道長。 經(jīng)常有香客問我赠法,道長麦轰,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任砖织,我火速辦了婚禮款侵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘侧纯。我一直安慰自己新锈,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布眶熬。 她就那樣靜靜地躺著壕鹉,像睡著了一般剃幌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晾浴,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼帕胆!你這毒婦竟也來了朝捆?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤懒豹,失蹤者是張志新(化名)和其女友劉穎芙盘,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脸秽,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡儒老,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了记餐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驮樊。...
- 正文 年R本政府宣布佳魔,位于F島的核電站,受9級特大地震影響晦炊,放射性物質(zhì)發(fā)生泄漏鞠鲜。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一断国、第九天 我趴在偏房一處隱蔽的房頂上張望贤姆。 院中可真熱鬧,春花似錦稳衬、人聲如沸霞捡。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽碧信。三九已至赊琳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間砰碴,已是汗流浹背躏筏。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像猖辫,于是被迫代替她去往敵國和親酥泞。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 1. 前言: 說點(diǎn)廢話啃憎,時(shí)間緊的請直接跳過芝囤,看后面的實(shí)現(xiàn)。 盡管本人很反感 C 語言中的宏定義辛萍,特別是濫用宏定義經(jīng)...
- 什么是迭代器和可迭代對象 迭代器:凡是實(shí)現(xiàn)了__iter__()和__next__()(python2.x是nex...
- 問題某軟件要求悯姊,從網(wǎng)絡(luò)抓取各個(gè)城市氣溫信息,并依次展示 如果一次抓取所有城市天氣再顯示叹阔,顯示第一個(gè)城市氣溫時(shí)挠轴,有很...
- c語言中的基本并不能滿足我們?nèi)粘J褂?這在其他面向?qū)ο蟮慕鉀Q方式是采用類定義的方式解決,那么在c語言中,giant...
- 自從8月22日晚上講了微課“從現(xiàn)在改變未來”传睹,到今天已經(jīng)是第十八天了耳幢,出乎大家意料的是,我居然在課后每天都花二到三...