为什么要使用queue.Peek(),而queue.First()/上次() [英] why to use queue.Peek() instead of queue.First()/Last()

查看:2537
本文介绍了为什么要使用queue.Peek(),而queue.First()/上次()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

队列包含特殊方法 queue.Peek(),我为什么要使用它时,我可以使用一般的 queue.First() queue.Last()

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

推荐答案

皮克是一个队列,一个经典的方法,其中的首先最后是LINQ的方法。由于队列最有可能的工具的IEnumerable 你的方法,第一个和最后它们没有什么相关性的严格的队列中。

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

这不是世界末日弯曲队列的法律。听起来你正在使用的结构,以满足您的需求。但谁需要一个队列像一个队列这可能是混乱到下一个程序员。也许你创建一个扩展队列类,称为StackedQueue。这时,如果有人需要保持你的代码,他们不会想到排队的行为。

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

这篇关于为什么要使用queue.Peek(),而queue.First()/上次()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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