我如何实施CopyTo?

时间:2013-08-21 14:18:12

标签: c#

我想知道我是否可以在ICollection中实现CopyTo。它声明它不使用ref关键字。我试过这个,但它没有编译

    public void CopyTo(ref KeyValuePair<TKey, TValue>[] destination, int start)
    {
        pairs.CopyTo(destination, start);
    }

它说:

  

错误4'Cyan.Collection.WatchableDictionary'未实现接口成员'System.Collections.Generic.ICollection&gt; .CopyTo(System.Collections.Generic.KeyValuePair [],int)'D:\ CE \ Supins \ Cyan Pembuat Soal \ Required Manipulation \ ObservableDictionary.cs 15 18所需的操作

但是如果我删除ref关键字,我担心如果它在其他ICollection-Implemented类中的行为不能正常。(实际上,我正在构建一个字典)

任何帮助都会被贬低。

如果错误在命名空间System.Collection中,请不要混淆。我在该命名空间中设计了我的代码。我刚将命名空间从System.Collection移动到Cyan.Collection。

1 个答案:

答案 0 :(得分:4)

没有ref它是正确的

public void CopyTo(KeyValuePair<TKey, TValue>[] destination, int start)
{
    pairs.CopyTo(destination, start);
}

调用者必须使用正确的大小创建数组,而不是方法。