C# 要将哪个集合用于最后操作的循环列表?

C# 要将哪个集合用于最后操作的循环列表?,c#,C#,我正在寻找一种允许我定义最大容量的C#收集类型。我想将对象添加到此集合,当我的容量达到时,最旧的对象应替换为新的对象 具体来说,我想创建一个收藏,记住我最后10或20个动作 我已经读过Google上的文章,但我正在寻找这个社区的答案。在.NET中唯一固定的集合类型是数组,因此它是唯一满足您要求的集合类型 您可以保留一个索引来进行旋转。你只需要记住下一个要写信的地点 大概是这样的: int index = 0; string[] collection = new string[10]; publ

我正在寻找一种允许我定义最大容量的C#收集类型。我想将对象添加到此集合,当我的容量达到时,最旧的对象应替换为新的对象

具体来说,我想创建一个收藏,记住我最后10或20个动作


我已经读过Google上的文章,但我正在寻找这个社区的答案。

在.NET中唯一固定的集合类型是数组,因此它是唯一满足您要求的集合类型

您可以保留一个索引来进行旋转。你只需要记住下一个要写信的地点

大概是这样的:

int index = 0;
string[] collection = new string[10];

public void Write(string text)
{
    index %= collection.Length; // prevent overflowing

    collection[index++] = text;
}

如果应用程序对性能不敏感,则可以使用通用集合
队列

下面是可以解决您的问题的示例包装器:

public class LimitedQueue<T>
{
    private readonly Queue<T> _queue;
    private readonly int _limit;

    public LimitedQueue(int limit)
    {
        _queue = new Queue<T>();
        _limit = limit;
    }

    public void Enqueue(T item)
    {
        if (_queue.Count == _limit) _queue.Dequeue();
        _queue.Enqueue(item);
    }

    public T Dequeue()
    {
        return _queue.Dequeue();
    }

    public T Peek()
    {
        return _queue.Peek();
    }

    public T[] GetAll()
    {
        return _queue.ToArray();
    }
}
public class LimitedQueue
{
专用只读队列_队列;
私有只读整数限制;
公共限制队列(整数限制)
{
_队列=新队列();
_极限=极限;
}
公共无效排队(T项)
{
如果(_queue.Count==_limit)_queue.Dequeue();
_排队。排队(项目);
}
公共T出列()
{
return_queue.Dequeue();
}
公共T Peek()
{
return_queue.Peek();
}
公共T[]GetAll()
{
return _queue.ToArray();
}
}

它的性能不如数组,但它可以让你做一些有用的事情,比如从
队列
中获取所有项目。你描述的类型我一直称为固定队列或固定大小的FIFO。想法是先进先出,但如果超出尺寸,则放弃先进先出:

public class FixedQueue<T>
{
    private readonly ConcurrentQueue<T> _innerQueue;
    private int _length;

    public FixedQueue(int length)
    {
        _length = length;
        _innerQueue = new ConcurrentQueue<T>(length);
    }  

    public void Enqueue(T obj)
    {
        lock (_innerQueue)
        {
            if (_innerQueue.Length == _length)
                _innerQueue.Dequeue();
            _innerQueue.Enqueue(obj);
        }
    }

    public T Dequeue()
    {
        lock (_innerQueue)
        {
            return _innerQueue.Dequeue();
        }
    }

    // etc...
}
公共类固定队列
{
私有只读ConcurrentQueue\u innerQueue;
私有整数长度;
公共固定队列(整数长度)
{
_长度=长度;
_innerQueue=新的ConcurrentQueue(长度);
}  
公共无效排队(T obj)
{
锁定(_innerQueue)
{
if(\u innerQueue.Length=\u Length)
_innerQueue.Dequeue();
_innerQueue.Enqueue(obj);
}
}
公共T出列()
{
锁定(_innerQueue)
{
返回_innerQueue.Dequeue();
}
}
//等等。。。
}

如果您正在寻找一种更面向对象的继承解决方案,而不关心维护,您可以编写以下代码:

public class FixedQueue<T> : Queue<T>
{
    //private readonly ConcurrentQueue<T> _innerQueue;
    private int _capacity;

    public FixedQueue(int capacity) : base()
    {
        _capacity = capacity;
    }

    public void Enqueue(T obj)
    {
        lock(this)
        {
            if (this.Count == _capacity)
                base.Dequeue();
            base.Enqueue(obj);
        }
    }

    public T Dequeue()
    {
        lock (this)
        {
            return this.Dequeue();
        }
    }
}
公共类固定队列:队列
{
//私有只读ConcurrentQueue\u innerQueue;
私人国际单位能力;
公共固定队列(int容量):base()
{
_容量=容量;
}
公共无效排队(T obj)
{
锁(这个)
{
if(this.Count==\u容量)
base.Dequeue();
基本排队(obj);
}
}
公共T出列()
{
锁(这个)
{
返回这个.Dequeue();
}
}
}

这个解决方案不尊重新的趋势,即你应该更喜欢组合而不是继承

这听起来像是一个相当简单的类型,所以向我们展示一个实现并询问相关问题会更有用。根据常见问题解答,询问库/工具是离题的。您的问题是讨论的主题。您要查找的类型称为