美文网首页
C# 自定义可迭代类型

C# 自定义可迭代类型

作者: 雪飞鸿 | 来源:发表于2020-03-31 07:13 被阅读0次
/// <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;
        }

    }

}

相关文章

  • C# 自定义可迭代类型

  • 迭代

    Python中自定义迭代类型,需要包含自定义的__next__方法。__next__方法能够返回迭代类型的下一项,...

  • 可迭代对象

    任何可迭代对象都可以作用于for循环,包括我们自定义的数据类型,只要符合迭代条件,就可以使用for循环。比如:字符...

  • 自定义异常

    ASP.NET MVC自定义异常处理 C#结构化异常之自定义异常 在C#中所有的异常类型都继承自System.Ex...

  • C#魔将-lesson_03-可空类型

    可空类型(Nullable) C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示...

  • python 三大神器

    迭代器1.1、判断可迭代对象1.2、自定义迭代器自定义迭代器对象: 在类里面提供iter和next方法创建的对象就...

  • Python高级特性与几种函数的讲解

    切片 从list或tuple中取部分元素。 iterable、iterator 可迭代,迭代器,集合类型数据可迭代...

  • C# 第二节

    C# 可空类型(Nullable) C# 单问号 ? 与 双问号 ?? ? :单问号用于对 int,double,...

  • python迭代器

    根据自定义一个可迭代器,学习迭代器协议,最简单的迭代器逻辑 输出如图:

  • Python高级知识点学习(六)

    Python中的迭代协议 迭代协议有两个概念: 可迭代类型(Iterable) 迭代器(Iterator) 迭代器...

网友评论

      本文标题:C# 自定义可迭代类型

      本文链接:https://www.haomeiwen.com/subject/kzppbctx.html