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
{
    /// 
    /// 表示对象的先进先出集合并固定长度
    /// 
    /// 
    public class QueueLength : Queue
    {
        int length = 1;

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

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

            base.Enqueue(item);
        }

    }
}

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

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

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

输出结果为:

 完成