C# net Queue 固定长度不自动扩展大小

C# net Queue  固定长度 不自动扩展大小 不可变大小

C# net 队列 固定长度 不自动扩展大小 不可变大小

新建文件 QueueLength.cs

拷贝下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Collections.Generic
{
    /// <summary>
    /// 表示对象的先进先出集合并固定长度
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class QueueLength<T> : Queue<T>
    {
        int length = 1;

        /// <summary>
        /// 初始化类的新实例,并指定长度
        /// </summary>
        /// <param name="length">长度</param>
        public QueueLength(int length) : base(length)
        {
            this.length = length;
        }

        /// <summary>
        /// 将对象添加到结尾处
        /// </summary>
        /// <param name="item">要添加的对象</param>
        public new void Enqueue(T item)
        {
            if (base.Count == length)
                base.Dequeue();

            base.Enqueue(item);
        }

    }
}

  

新建控制台拷贝如下代码测试

            QueueLength<int> aaaa = new QueueLength<int>(3);
            aaaa.Enqueue(1);
            aaaa.Enqueue(2);
            aaaa.Enqueue(3);
            aaaa.Enqueue(4);
            aaaa.Enqueue(5);

            Console.WriteLine(string.Join(",", aaaa));

  

输出结果为:

 完成

如有问题请联系QQ: var d=["1","2","3","4","5","6","7","8","9"]; var pass=d[8]+d[6]+d[0]+d[8]+d[2]+d[0]+d[4]+d[3]+d[2];
原文地址:https://www.cnblogs.com/ping9719/p/15775235.html