Why do I need to translate from ConcurrentDictionary to IDictionary to use Add ()?

If I have ConcurrentDictionaryand want to use a function Add(), I need to send to IDictionary:

var cd = new ConcurrentDictionary<int, int>();
cd.Add(1, 1);  // Compile error: does not contain a definition for 'Add'
IDictionary<int, int> id = cd;
id.Add(1, 1);  // Works

The question of ConcurrentDictionary and IDictionary tells me this because it ConcurrentDictionaryexplicitly implements IDictionaryusing private methods.

My question is: Why does ConcurrentDictionaryit work like this? What is the advantage of hiding the use of an implemented interface?

+4
source share
2 answers

My question is: Why is ConcurrentDictionary implemented like this?

, concurrency - . , , .

  • , indexer
  • , , AddOrUpdate
  • , , , TryAdd

Add, .

+5

, TryAdd() AddOrUpdate() ConcurrentDictionay, .

http://msdn.microsoft.com/es-es/library/dd287191(v=vs.110).aspx

0

Source: https://habr.com/ru/post/1527445/


All Articles