限制泛型集合的大小? [英] Limit the size of a generic collection?

查看:194
本文介绍了限制泛型集合的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来限制泛型集合的大小?

Is there any way to limit the size of a generic collection?

我有WriteableBitmap的一叠里面我是用存储在每一个WriteableBitmap的的克隆改变,这意味着我可以通过简单跳跳最新WriteableBitmap的出栈轻易撤销。

I have a Stack of WriteableBitmap which I am using to store a clone of a WriteableBitmap on each change, meaning that I can undo easily by simply Popping the most recent WriteableBitmap off the stack.

问题是内存使用情况,我想限制这个栈持有10对象,但我看不到属性让我很容易地做到这一点。有没有一种方法,还是我将不得不检查每个更改堆栈大小,最后10个对象复制到一个新的堆栈,每当我打10,并在以后每次的变化?我知道如何做到这一点,但希望有一个更简单的方法,是吗?

The problem is the memory usage, I want to limit this stack to hold 10 objects, but I can't see a property allowing me to easily do this. Is there a way, or am I going to have to check the stack size on each change, and copy the last 10 objects into a new stack whenever I hit 10, and on each subsequent change? I know how to do this, but was hoping that there was an easier way, is there?

推荐答案

您必须实现您的自己的包装,以实现这一目标。没有可用的直接选择。

You have to implement your own wrapper to achieve that. There is no direct option available.

class FixedSizeStack : Stack
{
    private int MaxNumber;
    public FixedSizeStack(int Limit)
        : base()
    {
        MaxNumber = Limit;
    }

    public override void Push(object obj)
    {
        if (this.Count < MaxNumber)
            base.Push(obj);
    }

}

这篇关于限制泛型集合的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆