Bug 16990 - ConcurrentDictionary lacks proper support for Contains(KeyValuePair<TKey, TValue>)
Summary: ConcurrentDictionary lacks proper support for Contains(KeyValuePair<TKey, TVa...
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2013-12-30 09:38 UTC by Adam Christie
Modified: 2014-05-30 09:18 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Adam Christie 2013-12-30 09:38:03 UTC
While porting a Conclave (https://github.com/guy-murphy/Conclave) based application to run under Mono-3.x, I noticed a difference in behaviour for one of the enumeration functions based on a ConcurrentDictionary:

Mono implementation from ConcurrentDictionary.cs:

        bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey, TValue> pair)
             return ContainsKey (pair.Key);

Expected implementation (from dotnetframework.org):

        bool ICollection<keyvaluepair<tkey, tvalue="">>.Contains(KeyValuePair<tkey, tvalue=""> keyValuePair)
            TValue value;
            if (!TryGetValue(keyValuePair.Key, out value))
                return false;
            return EqualityComparer<tvalue>.Default.Equals(value, keyValuePair.Value);

This difference means that when a KeyValuePair is submitted to Contains, then it will return True if just the key exists in the concurrent dictionary, instead of checking both whether the key exists and then checking the equality of the value.

I am using it in the following context:

        IEnumerable<KeyValuePair<string, string>> matchingAllParameters =
               new ImmutableDictionary<string, string>(new Dictionary<string, string> { { "action", "true" } });

        ConcurrentDictionary<string, string> parameters = new ConcurrentDictionary<string, string>
               { { "action", "false" } };

        bool matched = matchingAllParameters.All(p => parameters.Contains(p));

on Windows, matched will be false.
on Mono, matched will be true.

Current work around is:

        bool matched = matchingAllParameters.All(p => context.Params.Keys.Contains(p.Key) && context.Params[p.Key] == p.Value);
Comment 1 marcos.henrich 2014-05-30 06:47:00 UTC
Hi Adam,

Thank you for the detailed bug report.

The pull request for this issue can be found in the link below.
Comment 2 marcos.henrich 2014-05-30 09:18:21 UTC
Fixed in master db71dba1b775009146a9edaa1ac42abc4c0aaceb.