这个派生的接口getter有什么含糊之处?

use*_*667 6 c# inheritance interface

我想添加这些接口:

public interface IIndexGettable<TKey, TValue>
{
    TValue this[TKey key]{ get; }
}

public interface IIndexSettable<TKey, TValue>
{
    TValue this[TKey key]{ set; }
}

public interface IIndexable<TKey, TValue> : IIndexGettable<TKey, TValue>, IIndexSettable<TKey, TValue>
{   
  // new TValue this[TKey key]{ get; set; } // as suggested by SO question 1791359 this will "fix" the issue.
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用IIndexablegetter时,我得到以下编译器错误:

The call is ambiguous between the following methods or properties: 'IIndexGettalbe<TKey, TValue>.this[Tkey]' and 'IIndexSettable<TKey, TValue>.this[TKey]'

这是我运行的代码:

public static class IIndexableExtensions
 {
    public static Nullable<TValue> AsNullableStruct<TKey, TValue>(this IIndexable<TKey, object> reader, TKey keyName) where TValue : struct
    {
        if (reader[keyName] == DBNull.Value) // error is on this line.
        {
            return null;
        }
        else
        {
            return (TValue)reader[keyName];
        }
    }   
 }
Run Code Online (Sandbox Code Playgroud)

难道它不能明确地继承IIndexGettable中的getter和IIndexSettable中的setter吗?

顺便说一下,我并没有试图让这种声音对语言设计产生煽动性.我确信这背后有一个很好的理由,我的目标是了解理解是什么原因来更好地理解语言.

sin*_*ash 2

尝试了下面的代码,效果很好。你的错误是在什么场景下发生的?这段代码中没有什么?

class Program
    {
        static void Main(string[] args)
        {
            Foo<int, string> foo = new Foo<int, string>();
            foo.dict.Add(1, "a");
            foo.dict.Add(2, "b");
            Console.WriteLine(((IIndexGettable<int, string>)foo)[1]);
            ((IIndexSettable<int, string>)foo)[3] = "c";
            Console.WriteLine(foo[3]);
            Console.ReadLine();
        }
    }

    public interface IIndexGettable<TKey, TValue>
    {
        TValue this[TKey key] { get; }
    }

    public interface IIndexSettable<TKey, TValue>
    {
        TValue this[TKey key] { set; }
    }

    public interface IIndexable<TKey, TValue> : IIndexGettable<TKey, TValue>, IIndexSettable<TKey, TValue>
    {

    }

    public class Foo<TKey, TValue> : IIndexable<TKey, TValue>
    {
        public Dictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
        public TValue this[TKey key]
        {
            get
            {
                return dict[key];
            }

            set
            {
                dict[key] = value;
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)