具有通用实现的单链表
Single Linked List with Generic Implementation
我创建了一个具有通用实现的单链表。但是 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 进行编译
谢谢。
只需从 Add
方法中删除 <T>
通用类型,因为您的 class 已经是通用类型。
class和方法可以有相同的泛型类型名称(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
这个:
public void Add<T>(MyNode<T> node)
意味着 T
覆盖了您的 class 级别 T 声明,因此编译器将它们视为不同的类型。删除 T
将起作用,因为很明显您需要 classes T
声明。
只是为了让您直观地看到它,这也可以工作(不要使用它):
public void Add<TOther>(MyNode<T> node) where TOther : T
由于您现在明确告诉编译器 TOther
是 T
类型或派生类型。
我创建了一个具有通用实现的单链表。但是 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 进行编译
谢谢。
只需从 Add
方法中删除 <T>
通用类型,因为您的 class 已经是通用类型。
class和方法可以有相同的泛型类型名称(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
这个:
public void Add<T>(MyNode<T> node)
意味着 T
覆盖了您的 class 级别 T 声明,因此编译器将它们视为不同的类型。删除 T
将起作用,因为很明显您需要 classes T
声明。
只是为了让您直观地看到它,这也可以工作(不要使用它):
public void Add<TOther>(MyNode<T> node) where TOther : T
由于您现在明确告诉编译器 TOther
是 T
类型或派生类型。