I'm new to generics and I'm having some trouble implementing a small self practice code.
I'm creating a linked list. I want it to store char or int values. So I decided to make the implementation generic:
public class Node<T> where T : struct, IConvertible
{
public Node<T> next = null;
public T data = default(T);
public Node(T value) { this.data = value; }
}
I have a method that creates the linked list by generating random values in the range [33,127), converting the value to the type specified by T (e.g. if 86 is generated and T is Char, then the value to be stored in the linked list node will be 'V'; If T is Int32, then value will simply be 86). I'm facing two problems:
static Node<IConvertible> CreateList<T>(int len) where T : struct, IConvertible
{
Random r = new Random((int)DateTime.Now.Ticks);
T value = (T)r.Next(33, 127); // Problem #1
Node<T> head = new Node<T>(value);
Node<T> n = head;
for (int i = 1; i < len; i++)
{
n.next = new Node<T>(value);
n = n.next;
}
return head; // Problem #2
}
These are the problems :
1) Normally this is possible: (int) value = (char) r.Next(33, 127). Why if T is of type Char, the compiler says "Cannot convert type 'int' to 'T'", even if I had specified "where T : struct, IConvertible"?
2) "Cannot implicitly convert type 'LinkedList.Node<T>
' to 'LinkedList.Node<System.IConvertible>
'" If T is either Int32 or Char and both of them implement IConvertible, what is the way to cast Node<Int32>
or Node<Char>
to Node<IConvertible>
?
Thanks a lot!