Изменение шаблона вызывает ошибку "Cannot implicitly convert type 'ExamList.Form1.SimpleElm' to 'T'" - C#
Формулировка задачи:
Здравствуйте. Возникла проблема. Имеется рабочий код вида
Но если попробовать применить шаблон и заменить, то начинает ругаться
Как это исправить?
public class SimpleElm
{
public string value;
public SimpleElm next;
public SimpleElm(string value)
{
this.value = value;
this.next = null;
}
}
public static class List
{
...
static public bool Delete (ref SimpleElm firstElm, int n)
{
SimpleElm currentElm = firstElm;
if (currentElm == null) return false;
if (n == 1)
{
firstElm = firstElm.next;
return true;
}
for (int i = 1; i < n - 1; i++)
{
if (currentElm.next == null) return false;
currentElm = currentElm.next;
}
if (currentElm.next == null) return false;
currentElm.next = currentElm.next.next;
return true;
}
}static public bool Delete<T> (ref T firstElm, int n) where T: SimpleElm
{
T currentElm = firstElm;
if (currentElm == null) return false;
if (n == 1)
{
firstElm = firstElm.next;
return true;
}
for (int i = 1; i < n - 1; i++)
{
if (currentElm.next == null) return false;
currentElm = currentElm.next;
}
if (currentElm.next == null) return false;
currentElm.next = currentElm.next.next;
return true;
}
Cannot implicitly convert type 'ExamList.Form1.SimpleElm' to 'T'. An explicit conversion exists (are you missing a cast?)
Решение задачи: «Изменение шаблона вызывает ошибку "Cannot implicitly convert type 'ExamList.Form1.SimpleElm' to 'T'"»
textual
Листинг программы
class List<T>
{
class Node
{
public T value { get; set; }
public Node Next { get; set; }
public Node(T value)
{
this.value = value;
}
}
private Node Head { get; set; }
public void Add(T item)
{
if (Head == null)
Head = new Node(item);
else
{
...
}
}
}