Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no IQueue<T> or IStack<T> interface in the .NET Framework? [closed]

Tags:

.net

I wanted to write an object that operated on a queue-like interface. I went searching for IQueue<T>, but came back surprisingly empty-handed.

There are the following collection-related interfaces:

  • ICollection<T>
  • IList<T>
  • IDictionary<TKey,TValue>
  • ISet<T>

What made these types of collections get well-defined interfaces? Why do stacks and queues not warrant an interface?

like image 851
Paul Turner Avatar asked Dec 06 '10 15:12

Paul Turner


People also ask

Does C# have Queue?

C# includes generic Queue<T> and non-generic Queue collection. It is recommended to use the generic Queue<T> collection.

What is Stack and Queue in C#?

Queue and Stack are collection objects in the System. Collection namespace. The Queue class tracks objects on First-in & First-Out basis, while the Stack class tracks objects on First-in & Last-out basis. By using public methods of both Queue & Stacks classes, we can move objects to different locations.

What is Queue in C# with example?

A Queue in C# represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting. The Queue<T> class in the System. Collection. Generic namespace represents a queue in C#, where T specifies the type of elements in the queue.

What is Queue Class in C#?

Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue .


2 Answers

Well, because there would be one to one mapping from those interfaces to classes that implement them (except latest ConcurrentQueue). But other mentioned interfaces have lots of classes that implement them.

Expanding a bit. We can't know real motivation of BCL team. But i have a feeling that when they created Queue and Stack those classes looked so trivial so they decided not to abstract them out to interfaces. Reason is obvious: classes looked so simple but complete at the same time so there was no reason to create different implementations of them in BCL. Then came ConcurrentQueue and previous statement became slightly incorrect.

As it pointed Queue and ConcurrentQueue don't have that much similar to make them implement single interface.

like image 192
Andrey Avatar answered Sep 20 '22 06:09

Andrey


I would guess that you don't see Stack or Queue interfaces is because of the natures of the collections. The collection interfaces that you listed which do have interfaces are random access while Stacks and Queues are not. They have to be accessed in a very particular manner (FIFO or LIFO) and therefore cannot be used generally. So it makes sense to just use those collections as generic implementations but not through interfaces. In other words, because of the constraints that LIFO/FIFO put on a collection, there is very little (or nothing) that can be done differently by a class that implemented a theoretical IStack or IQueue.

To exercise this idea you could create your own IStack and/or IQueue and then implement them in a class. You will see that the functionality you expose will be completely pass-through. E.g. You will end up simply forwarding or returning requests and results to an internal Stack or Queue collection. And there will be little or nothing of potential value-added in that pass-through functionality.

like image 23
Paul Sasik Avatar answered Sep 19 '22 06:09

Paul Sasik