Contains Method

CounterCreationDataCollection.Contains Method (CounterCreationData)


Determines whether a CounterCreationData instance exists in the collection.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public bool Contains(
	CounterCreationData value


Type: System.Diagnostics.CounterCreationData

The CounterCreationData object to find in the collection.

Return Value

Type: System.Boolean

true if the specified CounterCreationData object exists in the collection; otherwise, false.

The following example demonstrates how to use the Contains method to find a CounterCreationData object in a CounterCreationDataCollection.

string myCategoryName;
int numberOfCounters;
Console.Write("Enter the category Name :");
myCategoryName = Console.ReadLine();
// Check if the category already exists or not.
if (!PerformanceCounterCategory.Exists(myCategoryName))
    Console.Write("Enter the number of counters : ");
    numberOfCounters = int.Parse(Console.ReadLine());
    CounterCreationData[] myCounterCreationData =
       new CounterCreationData[numberOfCounters];
    for (int i = 0; i < numberOfCounters; i++)
        Console.Write("Enter the counter name for {0} counter : ", i);
        myCounterCreationData[i] = new CounterCreationData();
        myCounterCreationData[i].CounterName = Console.ReadLine();
    CounterCreationDataCollection myCounterCollection =
       new CounterCreationDataCollection();
    // Add the 'CounterCreationData[]' to 'CounterCollection'.

       "Sample Category",
    PerformanceCounterCategoryType.SingleInstance, myCounterCollection);

    if (myCounterCreationData.Length > 0)
        if (myCounterCollection.Contains(myCounterCreationData[0]))
            Console.WriteLine("'{0}' counter is removed from the " +
               "CounterCreationDataCollection", myCounterCreationData[0].CounterName);
        Console.WriteLine("The counters does not exist");
    Console.WriteLine("The category already exists");

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft