When to use the Stack collection in C#?

后端 未结 12 2275
滥情空心
滥情空心 2021-02-14 20:44

I do understand how Stack() and Stack works, but I really can\'t see any scenarios where an array, List or IEnumer

12条回答
  •  庸人自扰
    2021-02-14 20:58

    Stack's functionality really seems like a subset of List(with a few renamed methods), so I agree it doesn't seem like the most useful collection by itself. When used internally in an algorithm, List can easily substitute for it, even if that may be slightly less idiomatic.

    Enforcing stack behavior is only necessary if it is exposed publicly. But it that case it's usually a better idea to expose some kind of wrapper over the internal collection, so it doesn't really seem very useful for that either.I'd certainly see use for a IStack interface, but not so much for a plain collection class Stack.

    My conclusion is that I wouldn't have included a Stack class in the framework, just a IStack interface. The BCL collections generally don't look very well thought out to me.

    ConcurrentStack on the other hand seems much more useful.

提交回复
热议问题