List of videos in .net

Is there any list / collection class in .NET that behaves like a rolling log file? The user can add items to it, but the list automatically removes old items if the maximum capacity is exceeded.

I also want to access any item in the list, eg. list [102], etc.

+3


source to share


3 answers


Here's a simple implementation for that:



public class LimitList<T> : IEnumerable<T>
{
    private LinkedList<T> _list = new LinkedList<T>();

    public LimitList(int maximumCount)
    {
        if (maximumCount <= 0)
            throw new ArgumentException(null, "maximumCount");

        MaximumCount = maximumCount;
    }

    public int MaximumCount { get; private set; }

    public int Count
    {
        get
        {
            return _list.Count;
        }
    }

    public void Add(T value)
    {
        if (_list.Count == MaximumCount)
        {
            _list.RemoveFirst();
        }
        _list.AddLast(value);
    }

    public T this[int index]
    {
        get
        {
            if (index < 0 || index >= Count)
                throw new ArgumentOutOfRangeException();

            return _list.Skip(index).First();
        }
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

      

+2


source


Microsoft's standard class doesn't exist for you. But you can take a look at the Queue <> class.
One problem is that Queue <> class auto expand. Can you solve this issue in this thread Limit <T> Queue Size in .NET?

Access to any element capable of the extended method. For example:



LogItem result = collection.Where(x => x.ID == 100).FirstOrDefault();

      

0


source


I would suggest using a registration framework like NLog .

0


source







All Articles