Reputation: 43
I am trying to print the value of list. But its just printing the value of of the address.My code looks like this.
class Program
{
static void Main(string[] args)
{
Node n = new Node("a", 12);
Node n1 = new Node("b", 13);
printNode(n);
List<Node> a = printNode(n1);
Console.WriteLine(a.ToString());
Console.ReadLine();
}
public static List<Node> printNode(Node n)
{
List<Node> a = new List<Node>();
a.Add(n);
return a;
}
}
My Node class goes like this.
class Node
{
public string value;
public double h;
public Node parent;
public Node(string val, double hVal)
{
value = val;
h = hVal;
}
public override string ToString()
{
return value;
}
}
How do I print [a,b] like this?
Upvotes: 4
Views: 1283
Reputation: 149656
Your code currently doesn't make much sense. You're passing a Node
to printNode
which actually creates a new list and returns it.
If you simply want to print items in your list, you'll need to iterate that list and print it's values:
Node n = new Node("a", 12);
Node n1 = new Node("b", 13);
List<Node> nodes = new List<Node> { n, n1 };
Console.WriteLine(string.Join(", ", nodes));
Console.ReadLine();
Here, string.Join
will iterate your list and join each value with the ","
separator.
Upvotes: 3