This documentation is archived and is not being maintained.

InstanceDataCollectionCollection.Contains Method

Determines whether an instance data collection for the specified counter (identified by one of the indexed InstanceDataCollection objects) exists in the collection.

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

public bool Contains(
	string counterName


Type: System.String
The name of the performance counter.

Return Value

Type: System.Boolean
true if an instance data collection containing the specified counter exists in the collection; otherwise, false.


The counterName parameter is null.

The counterName parameter is case-insensitive.

Each InstanceDataCollection object in the collection contains the performance data for all counters for an instance. The data is indexed by counter name and then by instance name. Contains returns true if there is an InstanceDataCollection object whose associated counter has the name specified by the counterName parameter.

The following code example accepts a PerformanceCounter category name and counter name from the command line. It gets the InstanceDataCollectionCollection for the PerformanceCounterCategory and then uses the Contains method to determine if the specified counter exists. If the counter exists, the example gets the associated InstanceDataCollection and displays the instance names from the collection.

public static void Main(string[] args)
    // The following values can be used as arguments.
    string categoryName = "Process";
    string counterName = "Private Bytes";

    InstanceDataCollectionCollection idColCol;

    // Copy the supplied arguments into the local variables.
        categoryName = args[0];
        counterName = args[1];
        // Ignore the exception from non-supplied arguments.

        // Get the InstanceDataCollectionCollection for this category.
        PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName);
        idColCol = pcc.ReadCategory();
    catch(Exception ex)
        Console.WriteLine("An error occurred getting the InstanceDataCollection for " +
            "category \"{0}\"."+ "\n" +ex.Message, categoryName);

    // Check if this counter name exists using the Contains
    // method of the InstanceDataCollectionCollection.
    if (!idColCol.Contains(counterName))
        Console.WriteLine("Counter \"{0}\" does not exist in category \"{1}\".", counterName, categoryName);
        // Now get the counter's InstanceDataCollection object using the
        // indexer (Item property) for the InstanceDataCollectionCollection.
        InstanceDataCollection countData = idColCol[counterName];

        ICollection idColKeys = countData.Keys;
        string[] idColKeysArray = new string[idColKeys.Count];
        idColKeys.CopyTo(idColKeysArray, 0);

        Console.WriteLine("Counter \"{0}\" of category \"{1}\" " +
            "has {2} instances.", counterName, categoryName, idColKeys.Count);

        // Display the instance names for this counter.
        int index;
        for(index=0; index<idColKeysArray.Length; index++)
            Console.WriteLine("{0,4} -- {1}", index+1, idColKeysArray[index]);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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