ContainsValue Method
.NET Framework Class Library
Dictionary<(Of <(TKey, TValue>)>)..::.ContainsValue Method

Determines whether the Dictionary<(Of <(TKey, TValue>)>) contains a specific value.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function ContainsValue ( _
    value As TValue _
) As Boolean
Visual Basic (Usage)
Dim instance As Dictionary
Dim value As TValue
Dim returnValue As Boolean

returnValue = instance.ContainsValue(value)
C#
public bool ContainsValue(
    TValue value
)
Visual C++
public:
bool ContainsValue(
    TValue value
)
JScript
public function ContainsValue(
    value : TValue
) : boolean

Parameters

value
Type: TValue
The value to locate in the Dictionary<(Of <(TKey, TValue>)>). The value can be nullNothingnullptra null reference (Nothing in Visual Basic) for reference types.

Return Value

Type: System..::.Boolean
true if the Dictionary<(Of <(TKey, TValue>)>) contains an element with the specified value; otherwise, false.

This method determines equality using the default equality comparer EqualityComparer<(Of <(T>)>)..::.Default for TValue, the type of values in the dictionary.

This method performs a linear search; therefore, the average execution time is proportional to Count. That is, this method is an O(n) operation, where n is Count.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
ContainsValue can use a different comparer than ContainsKey      David M. Kean - MSFT   |   Edit   |   Show History

This method uses the default comparer returned by EqualityComparer<T>.Default, which uses an ordinal comparison for strings. If you passed a specified comparer when you constructed the dictionary, ContainsValue could return different results than from ContainsKey.

For example, the following code shows this behavior:

[C#]
 
using System;
using System.Collections.Generic;
 
namespace Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.CurrentCulture);
            dictionary.Add("Encyclopædia", "Encyclopædia");
            
            Console.WriteLine(dictionary.ContainsKey("Encyclopaedia"));
            Console.WriteLine(dictionary.ContainsValue("Encyclopaedia"));
        }
    }
}

The above outputs the following:

True
False

As you can see, ContainsKey and ContainsValue are returning different results for the same search string. This is because ContainsKey is using a culture-sensitive comparison, whereas, ContainsValue is using an ordinal comparison.

As a workaround to get consistent results, you could define an extension method on IDictionary<TKey, TValue> that allows you to specify a comparer:

[C#]

using System;
using System.Collections.Generic;

namespace Samples
{
public static class DictionaryExtensions
{
public static bool ContainsValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value, IEqualityComparer<TValue> comparer)
{
if (comparer == null)
comparer = EqualityComparer<TValue>.Default;

foreach (var pair in dictionary)
{
if (comparer.Equals(pair.Value, value))
return true;
}

return false;
}
}
}

The above extension method, then allows you to do the following:

[C#]

using System;
using System.Collections.Generic;

namespace Samples
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.CurrentCulture);
dictionary.Add("Encyclopædia", "Encyclopædia");

Console.WriteLine(dictionary.ContainsKey("Encyclopaedia"));
Console.WriteLine(dictionary.ContainsValue("Encyclopaedia", StringComparer.CurrentCulture));
}
}
}

The above outputs the following:

True
True

Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker