C # property not available in derived class

I'm not sure what is going on. I have the following base class:

public class MyRow : IStringIndexable, System.Collections.IEnumerable,
    ICollection<KeyValuePair<string, string>>,
    IEnumerable<KeyValuePair<string, string>>,
    IDictionary<string, string>
{
    ICollection<string> IDictionary<string, string>.Keys { }
}

And then I have this derived class:

public class MySubRow : MyRow, IXmlSerializable, ICloneable,
    IComparable, IEquatable<MySubRow>
{
    public bool Equals(MySubRow other)
    {
        // "MyRow does not contain a definition for 'Keys'"
        foreach (string key in base.Keys) { }
    }
}

Why am I getting this error? "MyNamespace.MyRow" does not contain a definition for "Keys". Both classes are in the namespace MyNamespace. I tried to access this.Keysand base.Keys, and none of them work out MySubRow. I tried to mark the property Keysas publicin MyRow, but got the "public" modifier is not valid for this element ", I think, because it needs to implement the interface.

+3
4

Keys . ( protected), IDictionary<string, string>.Keys Keys .

public ICollection<string> Keys { ... }

protected ICollection<string> Keys { ... }

base IDictionary<string, string>:

((IDictionary<string, string>)base).Keys

( , , , , )

# : . :

public interface IMyInterface
{
    void Foo();
}

- , , . , Foo, . , public, , :

public class MyClass : IMyInterface
{
    public void Foo() { }
}

, public , . , . , private:

public class MyClass : IMyInterface
{
    void IMyInterface.Foo() { }
}

MyClass, , IMyInterface. :

void Bar()
{
    MyClass class1 = new MyClass();
    IMyInterface class2 = new MyClass();

    class1.Foo(); // works only in the first implementation style
    class2.Foo(); // works for both
}

. , , . , API .

+7

IDictionary < TKey, TValue > , this IDictionary<string,string>:

public bool Equals(MySubRow other)
{
    foreach (string key in ((IDictionary<string,string>)this).Keys) { }
}
+3

, : is explitcity , . :

public class MyRow : IStringIndexable, System.Collections.IEnumerable,
    ICollection<KeyValuePair<string, string>>,
    IEnumerable<KeyValuePair<string, string>>,
    IDictionary<string, string>
{
    ICollection<string> Keys { }
}
0

protected , ,

0

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


All Articles