이 문서는 기계 번역을 이용하여 번역되었습니다. 문서를 영문으로 보려면 영문 확인란을 선택하세요. 마우스 포인터를 텍스트 위로 이동시켜 팝업 창에서 영문 텍스트를 표시할 수도 있습니다.
번역
영문

Stack<T>.Clear 메서드 ()

 

게시 날짜: 2016년 11월

Stack<T>에서 개체를 모두 제거합니다.

네임스페이스:   System.Collections.Generic
어셈블리:  System(System.dll에 있음)

public void Clear()

Count is set to zero, and references to other objects from elements of the collection are also released.

The capacity remains unchanged. To reset the capacity of the T:System.Collections.Generic.Stack`1, call M:System.Collections.Generic.Stack`1.TrimExcess. Trimming an empty T:System.Collections.Generic.Stack`1 sets the capacity of the T:System.Collections.Generic.Stack`1 to the default capacity.

This method is an O(n) operation, where n is P:System.Collections.Generic.Stack`1.Count.

The following code example demonstrates several methods of the T:System.Collections.Generic.Stack`1 generic class, including the M:System.Collections.Generic.Stack`1.Clear method.

The code example creates a stack of strings with default capacity and uses the M:System.Collections.Generic.Stack`1.Push(`0) method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The M:System.Collections.Generic.Stack`1.Pop method is used to pop the first string off the stack. The M:System.Collections.Generic.Stack`1.Peek method is used to look at the next item on the stack, and then the M:System.Collections.Generic.Stack`1.Pop method is used to pop it off.

The M:System.Collections.Generic.Stack`1.ToArray method is used to create an array and copy the stack elements to it, then the array is passed to the M:System.Collections.Generic.Stack`1.#ctor(System.Collections.Generic.IEnumerable{`0}) constructor that takes T:System.Collections.Generic.IEnumerable`1, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.

An array twice the size of the stack is created, and the M:System.Collections.Generic.Stack`1.CopyTo(`0[],System.Int32) method is used to copy the array elements beginning at the middle of the array. The M:System.Collections.Generic.Stack`1.#ctor(System.Collections.Generic.IEnumerable{`0}) constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.

The M:System.Collections.Generic.Stack`1.Contains(`0) method is used to show that the string "four" is in the first copy of the stack, after which the M:System.Collections.Generic.Stack`1.Clear method clears the copy and the P:System.Collections.Generic.Stack`1.Count property shows that the stack is empty.

using System;
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Stack<string> numbers = new Stack<string>();
        numbers.Push("one");
        numbers.Push("two");
        numbers.Push("three");
        numbers.Push("four");
        numbers.Push("five");

        // A stack can be enumerated without disturbing its contents.
        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine("\nPopping '{0}'", numbers.Pop());
        Console.WriteLine("Peek at next item to destack: {0}", 
            numbers.Peek());
        Console.WriteLine("Popping '{0}'", numbers.Pop());

        // Create a copy of the stack, using the ToArray method and the
        // constructor that accepts an IEnumerable<T>.
        Stack<string> stack2 = new Stack<string>(numbers.ToArray());

        Console.WriteLine("\nContents of the first copy:");
        foreach( string number in stack2 )
        {
            Console.WriteLine(number);
        }

        // Create an array twice the size of the stack and copy the
        // elements of the stack, starting at the middle of the 
        // array. 
        string[] array2 = new string[numbers.Count * 2];
        numbers.CopyTo(array2, numbers.Count);

        // Create a second stack, using the constructor that accepts an
        // IEnumerable(Of T).
        Stack<string> stack3 = new Stack<string>(array2);

        Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
        foreach( string number in stack3 )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine("\nstack2.Contains(\"four\") = {0}", 
            stack2.Contains("four"));

        Console.WriteLine("\nstack2.Clear()");
        stack2.Clear();
        Console.WriteLine("\nstack2.Count = {0}", stack2.Count);
    }
}

/* This code example produces the following output:

five
four
three
two
one

Popping 'five'
Peek at next item to destack: four
Popping 'four'

Contents of the first copy:
one
two
three

Contents of the second copy, with duplicates and nulls:
one
two
three




stack2.Contains("four") = False

stack2.Clear()

stack2.Count = 0
 */

유니버설 Windows 플랫폼
8 이후 사용 가능
.NET Framework
2.0 이후 사용 가능
이식 가능한 클래스 라이브러리
이식 가능한 .NET 플랫폼 에서 지원됨
Silverlight
2.0 이후 사용 가능
Windows Phone Silverlight
7.0 이후 사용 가능
Windows Phone
8.1 이후 사용 가능
맨 위로 이동
표시: