Limit size of Queuelt;Tgt; in .NET?(队列的限制大小lt;Tgt;在.NET 中?)
问题描述
我有一个队列<T>我已将其初始化为容量 2 的对象,但显然这只是容量,并且在我添加项目时它会不断扩展.是否已经有一个对象可以在达到限制时自动将项目出列,或者是创建我自己的继承类的最佳解决方案?
I have a Queue<T> object that I have initialised to a capacity of 2, but obviously that is just the capacity and it keeps expanding as I add items. Is there already an object that automatically dequeues an item when the limit is reached, or is the best solution to create my own inherited class?
推荐答案
我已经完成了我正在寻找的基本版本,它并不完美,但它会完成这项工作,直到出现更好的东西.
I've knocked up a basic version of what I'm looking for, it's not perfect but it'll do the job until something better comes along.
public class LimitedQueue<T> : Queue<T>
{
public int Limit { get; set; }
public LimitedQueue(int limit) : base(limit)
{
Limit = limit;
}
public new void Enqueue(T item)
{
while (Count >= Limit)
{
Dequeue();
}
base.Enqueue(item);
}
}
这篇关于队列的限制大小<T>在.NET 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:队列的限制大小<T>在.NET 中?


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01