C# 自定義可迭代類型

/// <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;
        }

    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枕磁,一起剝皮案震驚了整個(gè)濱河市宪迟,隨后出現(xiàn)的幾起案子习贫,更是在濱河造成了極大的恐慌,老刑警劉巖汇陆,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件孕荠,死亡現(xiàn)場離奇詭異叨橱,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)畦韭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門疼蛾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人艺配,你說我怎么就攤上這事察郁。” “怎么了转唉?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵皮钠,是天一觀的道長。 經(jīng)常有香客問我赠法,道長麦轰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任砖织,我火速辦了婚禮款侵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘侧纯。我一直安慰自己新锈,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布眶熬。 她就那樣靜靜地躺著壕鹉,像睡著了一般剃幌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晾浴,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天负乡,我揣著相機(jī)與錄音,去河邊找鬼脊凰。 笑死抖棘,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的狸涌。 我是一名探鬼主播切省,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼帕胆!你這毒婦竟也來了朝捆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤懒豹,失蹤者是張志新(化名)和其女友劉穎芙盘,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脸秽,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡儒老,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了记餐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驮樊。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖片酝,靈堂內(nèi)的尸體忽然破棺而出囚衔,到底是詐尸還是另有隱情,我是刑警寧澤雕沿,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布佳魔,位于F島的核電站,受9級特大地震影響晦炊,放射性物質(zhì)發(fā)生泄漏鞠鲜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一断国、第九天 我趴在偏房一處隱蔽的房頂上張望贤姆。 院中可真熱鬧,春花似錦稳衬、人聲如沸霞捡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽碧信。三九已至赊琳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間砰碴,已是汗流浹背躏筏。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留呈枉,地道東北人趁尼。 一個(gè)月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像猖辫,于是被迫代替她去往敵國和親酥泞。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容