C # Generics Tips (3)

Source: Internet
Author: User
Tags data structures

4.5 Replace stack and queue with the appropriate generic version

Problem

You want to increase the efficiency of your application by replacing all stack and queue objects with the appropriate generic versions, and make your code easier to use. This is necessary when a struct or other value type is stored in these data structures, causing a boxing/unboxing operation.

Solution

Use System.Collections.Generic.Stack and System.Collections.Generic.Queue objects to replace existing System.Collections.Stack and System.Collections.Qu Eue object.

Here's a simple example of using the System.Collections.Queue object simply:

public static void UseNonGenericQueue()
  {
    // 创建一个非泛型队列对象
    Queue numericQueue = new Queue();
    // 进队(导致装箱操作).
    numericQueue.Enqueue(1);
    numericQueue.Enqueue(2);
    numericQueue.Enqueue(3);
    //出队并显示项(导致拆箱操作)
    Console.WriteLine(numericQueue.Dequeue());
    Console.WriteLine(numericQueue.Dequeue());
    Console.WriteLine(numericQueue.Dequeue().ToString());
}

The following is the same code using the System.Collections.Generic.Queue object

public static void UseGenericQueue()
  {
    // 创建一个泛型队列对象.
    Queue<int> numericQueue = new Queue<int>();
    // 进队.
    numericQueue.Enqueue(1);
    numericQueue.Enqueue(2);
    numericQueue.Enqueue(3);
    // 出队并显示项目.
    Console.WriteLine(numericQueue.Dequeue());
    Console.WriteLine(numericQueue.Dequeue());
    Console.WriteLine(numericQueue.Dequeue());
}

Here is an example of simply using the System.Collections.Stack object

public static void UseNonGenericStack()
  {
    // 创建一个非泛型栈.
    Stack numericStack = new Stack();
    // 进栈(导致装箱操作).
    numericStack.Push(1);
    numericStack.Push(2);
    numericStack.Push(3);
    // 出栈并显示项目(导致拆箱操作).
    Console.WriteLine(numericStack.Pop().ToString());
    Console.WriteLine(numericStack.Pop().ToString());
    Console.WriteLine(numericStack.Pop().ToString());
}
下面是相同的代码使用了System.Collections.Generic.Stack对象
public static void UseGenericStack()
  {
    // 创建一个泛型栈对象.
    Stack<int> numericStack = new Stack<int>();
    // 进栈.
    numericStack.Push(1);
    numericStack.Push(2);
    numericStack.Push(3);
    // 出栈并显示项目.
    Console.WriteLine(numericStack.Pop().ToString());
    Console.WriteLine(numericStack.Pop().ToString());
    Console.WriteLine(numericStack.Pop().ToString());
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.