当前位置: 首页>>代码示例>>C#>>正文


C# Queue.TrimExcess方法代码示例

本文整理汇总了C#中Queue.TrimExcess方法的典型用法代码示例。如果您正苦于以下问题:C# Queue.TrimExcess方法的具体用法?C# Queue.TrimExcess怎么用?C# Queue.TrimExcess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Queue的用法示例。


在下文中一共展示了Queue.TrimExcess方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: QueueExplore

        public void QueueExplore()
        {
            var queue = new Queue<string>();
            queue.Enqueue("1");
            queue.Enqueue("2");

            Assert.That(queue.Dequeue(), Is.EqualTo("1"));
            Assert.That(queue.Count, Is.EqualTo(1));
            queue.Enqueue("3");
            // you CAN loop through a queue as it implements IEnumerable
            foreach (var number in queue)
            {
                Console.WriteLine(number);
            }

            // original queue would have memory for 3, decrease it to only use 2 (same as count)
            queue.TrimExcess();
        }
开发者ID:jeremybreytenbach,项目名称:MCSD-Test,代码行数:18,代码来源:QueueTests.cs

示例2: Main

        static void Main(string[] args)
        {
            Queue<DateTime> aq = new Queue<DateTime>();
            aq.Enqueue(new DateTime(2008, 1, 19));   // item 1
            aq.Enqueue(new DateTime(2008, 4, 5));   // item 2
            aq.Enqueue(new DateTime(2008, 7, 2));   // item 3
            aq.Enqueue(new DateTime(2008, 12, 3));   // item 4

            Console.WriteLine("Count:    {0}", aq.Count);
            PrintValues(aq);

            // Peek item 1 but do not remove
            DateTime item = aq.Peek();
            Console.WriteLine("Peek: {0}", item.ToShortDateString());   // item is DateTime, not object
            PrintValues(aq);

            // Contains
            Boolean contains = aq.Contains(new DateTime(2008, 7, 2));
            Console.WriteLine("Contains: {0}", contains);

            // Remove items
            DateTime item1 = aq.Dequeue();
            DateTime item2 = aq.Dequeue();
            Console.WriteLine("Dequeue: {0}  {1}", item2.ToShortDateString(), item2.ToShortDateString());
            PrintValues(aq);
            Console.WriteLine("Count:    {0}", aq.Count);

            // Trim to size
            aq.TrimExcess();   // c.f. TrimToSize in non-generic Queue

            // iterate through Queue using a Queue.Enumerator
            Console.WriteLine("Listing using Queue.Enumerator");
            Queue<DateTime>.Enumerator qenum = aq.GetEnumerator();    // Queue<T>.Enumerator is a public inner struct declared in Queue<t>, see MyClass in this project for similar example
            while (qenum.MoveNext())
            {
                DateTime dt = qenum.Current;
                Console.WriteLine("{0}", dt.ToShortDateString());
            }
        }
开发者ID:BigBearGCU,项目名称:FNDEVModule2ExampleCode,代码行数:39,代码来源:Program.cs

示例3: DequeueFrom

        private ConcurrencyBehavior.IWaiter DequeueFrom(Queue<ConcurrencyBehavior.IWaiter> queue)
        {
            ConcurrencyBehavior.IWaiter waiter = queue.Dequeue();

            if (queue.Count == 0)
            {
                queue.TrimExcess();
            }

            return waiter;
        }
开发者ID:shijiaxing,项目名称:wcf,代码行数:11,代码来源:ConcurrencyBehavior.cs

示例4: TrimExcess

 public void TrimExcess(int items, int capacity)
 {
     var q = new Queue<int>(capacity);
     for (int i = 0; i < items; i++)
     {
         q.Enqueue(i);
     }
     q.TrimExcess();
     Assert.Equal(items, q.Count);
     Assert.Equal(Enumerable.Range(0, items), q);
 }
开发者ID:natemcmaster,项目名称:corefx,代码行数:11,代码来源:QueueTests.cs

示例5: Undo

        internal static void Undo( Player player, Command command ) {
            if( player.UndoBuffer.Count > 0 ) {
                // no need to set player.drawingInProgress here because this is done on the user thread
                Logger.Log( "Player {0} initiated /undo affecting {1} blocks (on world {2})", LogType.UserActivity,
                            player.Name,
                            player.UndoBuffer.Count,
                            player.World.Name );
                player.MessageNow( "Restoring {0} blocks...", player.UndoBuffer.Count );
                Queue<BlockUpdate> redoBuffer = new Queue<BlockUpdate>();
                while( player.UndoBuffer.Count > 0 ) {
                    BlockUpdate newBlock = player.UndoBuffer.Dequeue();
                    BlockUpdate oldBlock = new BlockUpdate( null, newBlock.X, newBlock.Y, newBlock.H,
                                                            player.World.Map.GetBlock( newBlock.X, newBlock.Y, newBlock.H ) );
                    player.World.Map.QueueUpdate( newBlock );
                    redoBuffer.Enqueue( oldBlock );
                }
                player.UndoBuffer = redoBuffer;
                redoBuffer.TrimExcess();
                player.MessageNow( "Type /undo again to reverse this command." );
                Server.RequestGC();

            } else {
                player.MessageNow( "There is currently nothing to undo." );
            }
        }
开发者ID:fragmer,项目名称:fCraft,代码行数:25,代码来源:BuildingCommands.cs


注:本文中的Queue.TrimExcess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。