I’m making PriorityQueue<T> classes, and right now I am using List<T> as a backing store. The following are the errors that I have been getting:
Have Correct Suffix/ Do not have incorrect suffix
Do not expose List<T>
For the -Queue suffix I need to inherit Queue<T>, but to sort a Queue<T>, I would need to empty the queue, sort, and then refill the queue. Also, if I inherit from Queue<T>, I would violate LSP because a priority queue is not a FIFO collection.
For one of the types of PriorityQueues that I am making is using an IComparer<T> to compare the elements, but IComparer<T> is only supported on arrays and List<T>.
I did see this question, but it doesnt fully relate to my question.
So here are my questions:
Should I suppress these code analysis warnings?
Should I inherit from Queue<T> and rewrite my classes to work off of it, even though it is less efficient?
If not, should I still swap out List<T> for something else?
Edit:
I don’t know if this would make any difference, but the following is my setup of each of my classes:
-
PriorityQueue<T>— abstract base class (Sort() is abstract) -
PriorityQueue<T, TComparer>— subclass that uses a comparer to
sort ReflectionPriorityQueue<T>— subclass that uses reflection to
sort, specifics not important to this question.
These errors have nothing to do with using a
List<T>– they’re about the public API.This one is debatable – personally, I’d disable this warning, as a
PriorityQueue<T>should, in my opinion, use the namePriorityQueue<T>.This just means you can’t publically expose the
List<T>as aList<T>. If you’re encapsulating the list, this should never appear. As long as you keep your list private, this warning should go away.I suspect the problem is that you’re trying to subclass
List<T>, which is a bad idea. Encapsulate it as a private member, and implement the appropriate interfaces instead.