Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why to use queue.Peek() instead of queue.First()/Last()

Tags:

c#

Queue contains special method queue.Peek(), why should I use it when I can use general queue.First() and queue.Last() ?

like image 676
Oleg Vazhnev Avatar asked Dec 14 '11 14:12

Oleg Vazhnev


1 Answers

Peek is a classic method of a queue, where as First and Last are Linq methods. Since the queue most likely implements IEnumerable you get the methods First and Last which have no relevance to an strict queue.


EDIT

It's not the end of the world to bend the law of a queue. Sounds like you are using the structure to fit your needs. It might be confusing to the next programmer though who expects a queue to act like a queue. Maybe you create a class that extends Queue, called a StackedQueue. Then if someone needs to maintain your code they won't expect a queue's behavior

like image 158
Joe Avatar answered Sep 24 '22 23:09

Joe