Skip to content

GetOrAdd extension method likely incorrectly appplying to ConcurrentDictionary #2931

@jaredpar

Description

@jaredpar

The DictionaryExtensions type defines the following GetOrAdd method

	public static TValue GetOrAdd<TKey, TValue>(
		this IDictionary<TKey, TValue> dictionary,
		TKey key,
		Func<TValue> newValue)
			where TKey : notnull

This means that it applies to ConcurrentDictionary<TKey, TValue> however the implementation of this extension method does not line up with the semantics of the actual GetOrAdd method. That method ensures that a key will only ever store a single value. The extension method though will override existing values if called concurrently.

That extension method is being called instead of the actual instance method in TestIntrospectionHelper. That is because the instance method overload that takes a delegate requires the signature Func<TKey, TValue> where as the calls in TestIntrospectionHelper pass no arguments hence go to this extension method.

Not sure this is causing an actual bug but seems incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions