c# - 具有通用实现的单链表

标签 c# generics linked-list

我已经创建了一个具有通用实现的单链表。但是 Add(..) 方法给出了编译错误:

Error 4 Cannot implicitly convert type 'ds.MyNode< T >' to 'ds.MyNode< T >'

代码实现如下:

public class MyNode<T>
{
    public MyNode(T content)
    {
        Content = content;
    }
    public T Content { get; set; }
    public MyNode<T> Next { get; set; }
}
public class MyLinkedList<T>
{
    private int size;
    private MyNode<T> head;
    private MyNode<T> tail;

    public MyNode<T> Tail
    {
        get { return tail; }
        set { tail = value; }
    }

    public int Count
    {
        get { return size; }
        set { size = value; }
    }

    public MyNode<T> Head
    {
        get { return head; }
        set { head = value; }
    }

    public void Add<T>(MyNode<T> node)
    {
        size++;
        if (head == null)
        {
            head = tail = node;
        }
        else
        {               
            tail.Next = node;
            tail = node;
        }
    }       
}

我不确定我在这里遗漏了什么,这个错误令人困惑,因为它说的两种类型都不能隐式转换是相同的。感谢您的帮助。

我正在针对 .Net 4.0 编译它

谢谢。

最佳答案

只需删除 <T>来自 Add 的通用类型方法,因为您的类已经是通用的。

类和方法可以具有相同的泛型类型名称 ( docs ):

If you define a generic method that takes the same type parameters as the containing class, the compiler generates warning CS0693 because within the method scope, the argument supplied for the inner T hides the argument supplied for the outer T. If you require the flexibility of calling a generic class method with type arguments other than the ones provided when the class was instantiated, consider providing another identifier for the type parameter of the method, as shown in GenericList2<T> in the following example.

class GenericList<T>
{
    // CS0693 
    void SampleMethod<T>() { }
}

class GenericList2<T>
{
    //No warning 
    void SampleMethod<U>() { }
}

因此,您可能应该启用编译警告。 Example of the compiler output from ideone.com :

prog.cs(39,22): warning CS0693: Type parameter `T' has the same name as the type parameter from outer type `Test.MyLinkedList<T>'
prog.cs(15,28): (Location of the symbol related to previous warning)
prog.cs(44,28): error CS0029: Cannot implicitly convert type `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
prog.cs(48,26): error CS0029: Cannot implicitly convert type `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
prog.cs(49,21): error CS0029: Cannot implicitly convert type `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
Compilation failed: 3 error(s), 1 warnings

关于c# - 具有通用实现的单链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31068315/

相关文章:

c# - 如何像 Microsoft Word 一样在 TextBox 中给拼写错误的单词加下划线?

c# - 使用 CompositionScopeDefinition 在 MEF 中定义范围

c# - C# 泛型可以有特定的基类型吗?

c - C链表遍历中移除条目并释放桶节点时出错

c# - MQTT Java 或 C# 服务器

c# - 字符串插值与 String.Format

typescript - 在 TypeScript 中,如何获取值为给定类型的对象类型的键?

C# : Extending Generic class

c - 使用 C 将 CFG 从文件读取到链表中

java - 为什么我的代码没有删除重复的节点?我的输出仍然是12311