类继承了通用字典< string,IFoo>和接口 [英] Class inherits generic dictionary<string, IFoo> and Interface

查看:160
本文介绍了类继承了通用字典< string,IFoo>和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类继承了一个通用字典和接口

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

问题是这个类的消费者正在寻找对于接口的'.Keys'和'.Values'属性,我添加了:

  ///< summary> 
///
///< / summary>
ICollection< string>钥匙{get; }

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

到界面。



现在,实现需要有这个,但是当我实现这些时,我得到这个错误:

关键字new是必需的,因为它隐藏了属性Keys ..



那么我需要做什么。我应该在这些get属性的前面添加一个new吗?

解决方案

另一种选择是更改接口为:

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

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

这种方式界面已经由字典实现,为您节省了麻烦再次实现这些属性,它不会隐藏或覆盖原始实现。


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?

解决方案

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.

这篇关于类继承了通用字典&lt; string,IFoo&gt;和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆