C
C#2d ago
Vortac

Properties referencing their class

Given the following class example:
csharp
class Node
{
public Node LeftNode { get; set; }
public Node RightNode { get; set; }
public int Data { get; set; }
}

csharp
class Node
{
public Node LeftNode { get; set; }
public Node RightNode { get; set; }
public int Data { get; set; }
}

How do the Node properties work here? Do they create new instances of the class?
3 Replies
Salman
Salman2d ago
yes ofc
var root = new Node(0);
root.Left = new Node(1);
root.Right = new Noe(2);
root.Left.Left = new Node(3);
var root = new Node(0);
root.Left = new Node(1);
root.Right = new Noe(2);
root.Left.Left = new Node(3);
sibber
sibber2d ago
theyre initialized to whatever 0 means for the type which is null for reference types and 0 for ints
Jimmacle
Jimmacle2d ago
(also the equivalent of 0 for any struct, including numbers)

Did you find this page helpful?