Skip to main content
.NET Framework Class Library
StackTContains Method

Determines whether an element is in the StackT.

Namespace:   System.Collections.Generic
Assembly:  System (in System.dll)
Syntax
Public Function Contains ( _
	item As T _
) As [%$TOPIC/xeaek790_en-us_VS_110_2_0_0_0_0%]
public [%$TOPIC/xeaek790_en-us_VS_110_2_0_1_0_0%] Contains(
	T item
)
public:
[%$TOPIC/xeaek790_en-us_VS_110_2_0_2_0_0%] Contains(
	T item
)
member Contains : 
        item:'T -> [%$TOPIC/xeaek790_en-us_VS_110_2_0_3_0_0%]

Parameters

item
Type: T

The object to locate in the StackT. The value can be for reference types.

Return Value

Type: SystemBoolean
true if item is found in the StackT; otherwise, false.
Remarks

This method determines equality using the default equality comparer EqualityComparerTDefault for T, the type of values in the list.

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

Examples

The following code example demonstrates several methods of the StackT generic class, including the Contains method.

The code example creates a stack of strings with default capacity and uses the Push 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 Pop method is used to pop the first string off the stack. The Peek method is used to look at the next item on the stack, and then the Pop method is used to pop it off.

The ToArray method is used to create an array and copy the stack elements to it, then the array is passed to the StackT constructor that takes IEnumerableT, 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 CopyTo method is used to copy the array elements beginning at the middle of the array. The StackT 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 Contains method is used to show that the string "four" is in the first copy of the stack, after which the Clear method clears the copy and the Count property shows that the stack is empty.

Imports System
Imports System.Collections.Generic

Module Example

    Sub Main

        Dim numbers As New Stack(Of 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. 
        For Each number As String In numbers
            Console.WriteLine(number)
        Next

        Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop())
        Console.WriteLine("Peek at next item to pop: {0}", _
            numbers.Peek())    
        Console.WriteLine("Popping '{0}'", numbers.Pop())

        ' Create another stack, using the ToArray method and the 
        ' constructor that accepts an IEnumerable(Of T). Note that 
        ' the order of items on the new stack is reversed. 
        Dim stack2 As New Stack(Of String)(numbers.ToArray())

        Console.WriteLine(vbLf & "Contents of the first copy:")
        For Each number As String In stack2
            Console.WriteLine(number)
        Next 

        ' Create an array twice the size of the stack, compensating 
        ' for the fact that Visual Basic allocates an extra array  
        ' element. Copy the elements of the stack, starting at the 
        ' middle of the array.  
        Dim array2((numbers.Count * 2) - 1) As String
        numbers.CopyTo(array2, numbers.Count)

        ' Create a second stack, using the constructor that accepts an 
        ' IEnumerable(Of T). The elements are reversed, with the null 
        ' elements appearing at the end of the stack when enumerated. 
        Dim stack3 As New Stack(Of String)(array2)

        Console.WriteLine(vbLf & _
            "Contents of the second copy, with duplicates and nulls:")
        For Each number As String In stack3
            Console.WriteLine(number)
        Next

        Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _
            stack2.Contains("four"))

        Console.WriteLine(vbLf & "stack2.Clear()")
        stack2.Clear()
        Console.WriteLine(vbLf & "stack2.Count = {0}", _
            stack2.Count)
    End Sub 
End Module 

' This code example produces the following output: 
' 
'five 
'four 
'three 
'two 
'one 
' 
'Popping 'five' 
'Peek at next item to pop: 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
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
 */
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.