I’m not sure what’s 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 do I get that error? “‘MyNamespace.MyRow’ does not contain a definition for ‘Keys'”. Both classes are in the MyNamespace namespace. I tried accessing this.Keys and base.Keys and neither works from within MySubRow. I tried marking the Keys property as public in MyRow but got “The modifier ‘public’ is not valid for this item”, I think because it’s necessary to implement an interface.
You’re implementing the
Keysproperty explicitly. If you want to make that member publicly accessible (orprotected), changeIDictionary<string, string>.KeystoKeysand add the appropriate visibility modifier in front of it.or
You could reference
baseas an instance ofIDictionary<string, string>as well:More Information
(Judging by your comments you appear to be familiar with the distinction, but others may not be)
C# interface implementation can be done two ways: implicitly or explicitly. Let’s consider this interface:
An interface is just a contract for what members a class must make available to code that is calling it. In this case, we have one function called
Foothat takes no parameters and returns nothing. An implicit interface implementation means that you must expose apublicmember that matches the name and signature of the member on the interface, like this:This satisfies the interface because it exposes a
publicmember on the class that matches every member on the interface. This is what is usually done. However, it is possible to explicitly implement the interface and map the interface function to aprivatemember:This creates a private function on
MyClassthat is only accessible to outside callers when they are referring to an instance ofIMyInterface. For instance:Explicit implementations are always private. If you want to expose it outside of the class you’ll have to create another member and expose that, then use the explicit implementation to call the other member. This is usually done so that a class can implement interfaces without cluttering up its public API, or if two interfaces expose members with the same name.