Generic Linked List in C#

Shervan360 1,481 Reputation points
2022-10-09T13:29:10.247+00:00

Hello,

How can I solve the error in Line 12?

using System.Collections.Generic;

class Node<T>  
{  
    public T Value { get; set; }  
    public Node<T> Next { get; set; }  
    public Node(T val) => Value = val;  
}  
  
class Program  
{  
    public static void Main()  
    {  
        LinkedList<Node> list = new(); //error  
  
        Node<int> first = new(10);  
        Node<string> second = new("Daniel");  
        Node<int> third = new(17);  
        Node<char> fourth = new('a');  
        Node<int> fifth = new(1);  
        Node<int> sixth = new(50);  
        // Node seventh = new(100);  
  
        //list.AddLast(first);  
        //list.AddLast(second);  
        //list.AddLast(third);  
        //list.AddLast(fourth);  
        //list.AddLast(fifth);  
        //list.AddLast(sixth);  
        //// list.AddLast(seventh);  
  
         
    }  
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2022-10-09T13:44:30.217+00:00

    You need to specify the type e.g. LinkedList<Node<string>> list = new();

    1 person found this answer helpful.
    0 comments No comments