views:

1910

answers:

4

I have a class that inherits a generic dictionary and an inteface

public class MyDictionary: Dictionary<string, IFoo>, IMyDictionary
{
}

the issue is that consumers of this class are looking for the '.Keys' and ".Values" properties of the interface so i added:

    /// <summary>
    /// 
    /// </summary>
    ICollection<string> Keys { get; }

    /// <summary>
    /// 
    /// </summary>
    IEnumerable<IFoo> Values { get; }

to the interface.

Now, the implementation needs to have this as well but when i implement these, i get this error:

"The keyword new is required because it hides property Keys . .. "

so what do i need to do. Should i be adding a "new" in front of these get properties?

A: 

The reason is that your keys and values properties are hiding the implementation of the keys and values properties in the Dictionary class.

EDIT: Yes all you have to do is add the new keyword into you property. So you code will look somthing like this:

class IFoo
{ }

interface MyDictionary
{
    ICollection<string> Keys { get; }

    /// <summary>
    /// 
    /// </summary>
    IEnumerable<IFoo> Values { get; }
}

class IStuff : Dictionary<string, IFoo>, MyDictionary
{
    #region MyDictionary Members

    //Note the new keyword.
    public new ICollection<string> Keys
    {
        get { throw new NotImplementedException(); }
    }

    public new IEnumerable<IFoo> Values
    {
        get { throw new NotImplementedException(); }
    }

    #endregion
}

I would still recommend implementing IDictionary though.

Nathan W
can you give me a code example ?
ooo
I'm working on it now.
Nathan W
+3  A: 

Another option would be to change the types on the interface to be:

public interface IMyDictionary
{
    /// <summary>
    /// 
    /// </summary>
    Dictionary<string, IFoo>.KeyCollection Keys { get; }

    /// <summary>
    /// 
    /// </summary>
    Dictionary<string, IFoo>.ValueCollection Values { get; }
}

That way the interface is already implemented by the dictionary saving you the trouble of implementing the properties again, and it doesn't hide or cover the original implementation.

Cameron MacFarland
I get this error: Error 2 Inconsistent accessibility: property type 'System.Collections.Generic.Dictionary<string,ClassLibrary1.IFoo>.KeyCollection' is less accessible than property 'ClassLibrary1.IMyDictionary.Keys'
Nathan W
Works fine on my machine. Make sure IFoo is public too.
Cameron MacFarland
And the IMyDictionary interface aswell
Cameron MacFarland
*slaps forehead* duh!!
Nathan W
:) See user icon
Cameron MacFarland
haha yeah it happens sometimes.
Nathan W
+2  A: 

Dictionary<string, IFoo> implements the IDictionary<TKey,TValue> interface which already provides the Keys and Values properties. There shouldn't be a need to create your own properties, but the way to get around the compiler warning is to add the new keyword at the beginning of your property declarations in your class.

Scott Dorman
+1  A: 

You could also use explicit interface implementation. That way if someone is referencing MyDictionary by using the interface IMyDictionary they will see the methods and nothing else. I would also recommend implementing IDictionary instead. Unless you want to restrict usage to just Keys and Values.

public class MyDictionary : Dictionary<string, IFoo>, IMyDictionary
{
    ICollection<string> IMyDictionary.Keys
    {
        get { return Keys; }
    }

    IEnumerable<IFoo> IMyDictionary.Values
    {
        get { return Values; }
    }
}
Mike Two