C#内置队列类Queue用法实例

 更新时间:2015年04月29日 10:42:15   作者:八大山人  
这篇文章主要介绍了C#内置队列类Queue用法,实例分析了C#内置队列的添加、移除等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#内置队列类Queue用法。分享给大家供大家参考。具体分析如下:

这里详细演示了C#内置的队列如何进行添加,移除等功能。

using System;
using System.Collections.Generic;
class Example
{
 public static void Main()
 {
  Queue<string> numbers = new Queue<string>();
  numbers.Enqueue("one");
  numbers.Enqueue("two");
  numbers.Enqueue("three");
  numbers.Enqueue("four");
  numbers.Enqueue("five");
  // A queue can be enumerated without disturbing its contents.
  foreach( string number in numbers )
  {
   Console.WriteLine(number);
  }
  Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
  Console.WriteLine("Peek at next item to dequeue: {0}", 
   numbers.Peek());
  Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
  // Create a copy of the queue, using the ToArray method and the
  // constructor that accepts an IEnumerable<T>.
  Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
  Console.WriteLine("\nContents of the first copy:");
  foreach( string number in queueCopy )
  {
   Console.WriteLine(number);
  }
  // Create an array twice the size of the queue and copy the
  // elements of the queue, starting at the middle of the 
  // array. 
  string[] array2 = new string[numbers.Count * 2];
  numbers.CopyTo(array2, numbers.Count);
  // Create a second queue, using the constructor that accepts an
  // IEnumerable(Of T).
  Queue<string> queueCopy2 = new Queue<string>(array2);
  Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
  foreach( string number in queueCopy2 )
  {
   Console.WriteLine(number);
  }
  Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}", 
   queueCopy.Contains("four"));
  Console.WriteLine("\nqueueCopy.Clear()");
  queueCopy.Clear();
  Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
 }
}
/* This code example produces the following output:
one
two
three
four
five
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Contents of the copy:
three
four
five
Contents of the second copy, with duplicates and nulls:
three
four
five
queueCopy.Contains("four") = True
queueCopy.Clear()
queueCopy.Count = 0
 */

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • 基于WPF实现3D导航栏控件

    基于WPF实现3D导航栏控件

    这篇文章主要介绍了如何基于WPF实现简单的3D导航栏控件效果,文中的示例代码讲解详细,对我们的学习或工作有一定帮助,需要的小伙伴可以参考一下
    2024-03-03
  • 轻松学习C#的ArrayList类

    轻松学习C#的ArrayList类

    轻松学习C#的ArrayList类,对C#的ArrayList类感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的ArrayList类
    2015-11-11
  • C# 预处理器指令的用法

    C# 预处理器指令的用法

    本文主要介绍了C# 预处理器指令的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • C#编程自学之数据类型和变量二

    C#编程自学之数据类型和变量二

    这篇文章继续介绍了C#数据类型和变量,是对上一篇文章的补充,希望对大家的学习有所帮助。
    2015-10-10
  • C#如何通过匿名类直接使用访问JSON数据详解

    C#如何通过匿名类直接使用访问JSON数据详解

    这篇文章主要给大家介绍了关于C#如何通过匿名类直接使用访问JSON数据的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
    2018-02-02
  • c#队列Queue学习示例分享

    c#队列Queue学习示例分享

    队列Queue,先进先出,先生产的货物先出货,后生产的货物后出货,下面看示例学习c#队列Queue
    2013-12-12
  • c#日志记录帮助类分享

    c#日志记录帮助类分享

    这篇文章主要介绍了c#日志记录帮助类,可以设置记录的日志类型,需要的朋友可以参考下
    2014-03-03
  • 桌面浮动窗口(类似恶意广告)的实现详解

    桌面浮动窗口(类似恶意广告)的实现详解

    本篇文章是对桌面浮动窗口的实现方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • winform 实现选择文件和选择文件夹对话框的简单实例

    winform 实现选择文件和选择文件夹对话框的简单实例

    下面小编就为大家带来一篇winform 实现选择文件和选择文件夹对话框的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • C# 通过反射初探ORM框架的实现原理(详解)

    C# 通过反射初探ORM框架的实现原理(详解)

    下面小编就为大家分享一篇C# 通过反射初探ORM框架的实现原理详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12

最新评论