Is there a way to get the last element in a Queue?

前端 未结 5 1388
鱼传尺愫
鱼传尺愫 2021-01-26 18:26

I know stack is best and easiest way, yet could it be possible to obtain the last element in a queue without having to dequeue anything?

5条回答
  •  再見小時候
    2021-01-26 19:18

    No, you would have to convert the queue to some other collection (using ToList or ToArray) in order to do this. This would effectively dequeue everything in the queue.

    Keep in mind that by needing the last item from a queue you have effectively proven that you are using the wrong collection to hold your data. Consider changing your collection to another type that provides all the operations you need.

提交回复
热议问题