前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 自定义可迭代类型

C# 自定义可迭代类型

作者头像
雪飞鸿
发布2020-04-01 17:28:52
3790
发布2020-04-01 17:28:52
举报
文章被收录于专栏:me的随笔me的随笔
/// <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>
    /// 可迭代对象中存储的对象数
    /// </summary>
    /// <value>auto-property</value>
    public int Count { get; private set; } = 0;

    public T this[int index] => _innerArray[index];

    /// <summary>
    /// 向可迭代对象中添加元素
    /// </summary>
    /// <remarks>当添加的元素数超过<see cref="_capacity"/>的值时,新添加的元素会覆盖之前的值</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;
        }

    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档