c# - 错误 : '<method1>' and '<method2>' cannot overload each other

标签 c# .net vb.net

我在 VB 中覆盖了一个列表。

在 C# 中,代码编译后看起来像这样:

class MyObjectCollection : IList
{
    ...
    /// <summary>
    /// Gets or sets the element at the specified index.
    /// </summary>
    public MyObject this[int index]
    {
        get { return (MyObject)innerArray[index]; }
        set { innerArray[index] = value; }
    }
    ...
}

在 VB.NET 中我转换:

Class MyObjectCollection
    Implements IList        
    ...

    ''' <summary> '
    ''' Gets or sets the element at the specified index. '
    ''' </summary> '
    Default Public Overrides Property Item(ByVal index As Integer) As MyObject
      Get
        Return DirectCast(innerArray(index), MyObject)
      End Get
      Set(ByVal value As MyObject)
        innerArray(index) = value
      End Set
    End Property
    ...
End Class

错误:

'Public Overrides Default Property Item(index As Integer) As MyObject' and 'Public Default Property Item(index As Integer) As Object' cannot overload each other because they differ only by return types

C# 中的整个集合类

public class MyObjectCollection : IList
{
    private ArrayList innerArray;

    public MyObjectCollection()
    {
        innerArray = new ArrayList();
    }

    public int Count
    {
        get { return innerArray.Count; }
    }

    public bool IsFixedSize
    {
        get { return false; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool IsSynchronized
    {
        get { return false; }
    }

    object ICollection.SyncRoot
    {
        get { return null; }
    }

    public MyObject this[int index]
    {
        get { return (MyObject)innerArray[index]; }
        set { innerArray[index] = value; }
    }

    public int Add(MyObject value)
    {
        int index = innerArray.Add(value);

        return index;
    }

    public void AddRange(MyObject[] array)
    {
        innerArray.AddRange(array);
    }

    public void Clear()
    {
        innerArray.Clear();
    }

    public bool Contains(MyObject item)
    {
        return innerArray.Contains(item);
    }

    public bool Contains(string name)
    {
        foreach (MyObject spec in innerArray)
            if (spec.Name == name)
                return true;

        return false;
    }

    public void CopyTo(MyObject[] array)
    {
        innerArray.CopyTo(array);
    }

    public void CopyTo(MyObject[] array, int index)
    {
        innerArray.CopyTo(array, index);
    }

    public IEnumerator GetEnumerator()
    {
        return innerArray.GetEnumerator();
    }

    public int IndexOf(MyObject value)
    {
        return innerArray.IndexOf(value);
    }

    public int IndexOf(string name)
    {
        int i = 0;

        foreach (MyObject spec in innerArray)
        {
            if (spec.Name == name)
                return i;

            i++;
        }

        return -1;
    }

    public void Insert(int index, MyObject value)
    {
        innerArray.Insert(index, value);
    }

    public void Remove(MyObject obj)
    {
        innerArray.Remove(obj);
    }

    public void Remove(string name)
    {
        int index = IndexOf(name);
        RemoveAt(index);
    }

    public void RemoveAt(int index)
    {
        innerArray.RemoveAt(index);
    }

    public MyObject[] ToArray()
    {
        return (MyObject[])innerArray.ToArray(typeof(MyObject));
    }

    #region Explicit interface implementations for ICollection and IList
    void ICollection.CopyTo(Array array, int index)
    {
        CopyTo((MyObject[])array, index);
    }

    int IList.Add(object value)
    {
        return Add((MyObject)value);
    }

    bool IList.Contains(object obj)
    {
        return Contains((MyObject)obj);
    }

    object IList.this[int index]
    {
        get
        {
            return ((MyObjectCollection)this)[index];
        }
        set
        {
            ((MyObjectCollection)this)[index] = (MyObject)value;
        }
    }

    int IList.IndexOf(object obj)
    {
        return IndexOf((MyObject)obj);
    }

    void IList.Insert(int index, object value)
    {
        Insert(index, (MyObject)value);
    }

    void IList.Remove(object value)
    {
        Remove((MyObject)value);
    }
    #endregion
}

最佳答案

我建议继承自 CollectionBase ,它专门用于创建强类型集合。

Public Class MyObjectCollection
   Inherits CollectionBase

检查 example .

关于c# - 错误 : '<method1>' and '<method2>' cannot overload each other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2744048/

相关文章:

vb.net - 使用 LDAP 将用户添加到 AD

c# - Json 单个对象字符串到 List<string>

c# - .NET 自定义配置 - 我可以拥有包含非同类元素的元素集合吗?

c# - 将 csv 读取到网格的更快方法

vb.net - 检测用户存在

.net - DataGridView 仅数字单元格?

c# - 使用 ExpressionVisitor 从表达式中删除条件

来自名称的 C# 颜色值

c# - C#中的堆栈容量

c# - 绘制 3D 风格效果时遇到问题