메서드에 두 개의 오버로드가있는 경우 을 수락하고 IDictionary<TKey, TValue>
을 수락하는 수식에 new Dictionary<string, int>()
을 전달하면 모호한 것으로 간주됩니다. 그러나 두 가지 오버로드가 IEnumerable
및 IEnumerable<KeyValuePair<TKey, TValue>>
을 허용하도록 변경되면 호출이 더 이상 모호하지 않습니다. Dictionary<TKey, TValue>
로서메소드에 IDictionary 및 IDictionary에 대한 오버로드가있는 경우 모호한 호출
은 (IEnumerable
, IEnumerable<KeyValuePair<TKey, TValue>>
, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
, IReadOnlyDictionary<TKey, TValue>
, ICollection
, IDictionary
, ICollection<KeyValuePair<TKey, TValue>>
, IDictionary<TKey, TValue>
정확한 것으로 .NET 4.5 ISerializable
, IDeserializationCallback
) 위의 모든 인터페이스를 구현; IDictionary
은 IEnumerable
에서 상속 받고 IDictionary<TKey, TValue>
은 IEnumerable<KeyValuePair<TKey, TValue>>
에서 상속되므로 왜 그런지 이해할 수 없습니다.
샘플 콘솔 응용 프로그램 :
using System;
using System.Collections;
using System.Collections.Generic;
namespace AmbigousCall
{
internal class Program
{
static void Main (string[] args)
{
var dic = new Dictionary<string, int>();
FooDic(dic); // Error: The call is ambiguous
FooEnum(dic); // OK: The generic method is called
Console.ReadKey();
}
static void FooDic (IDictionary dic) {}
static void FooDic<TKey, TValue> (IDictionary<TKey, TValue> dic) {}
static void FooEnum (IEnumerable dic) {}
static void FooEnum<TKey, TValue> (IEnumerable<KeyValuePair<TKey, TValue>> dic) {}
}
}
내가 오류는 다음과 같습니다 호출은 다음과 같은 방법 또는 속성 사이의 모호 'AmbigousCall.Program.FooDic (System.Collections.IDictionary)'와 'AmbigousCall .Program.FooDic (System.Collections.Generic.IDictionary) '
질문 1 : 왜 발생합니까?
질문 2 : 클래스가 둘 다 구현하는 경우 모호하지 않게 일반 및 일반이 아닌 인수를 모두 받아들이는 방법?