1
public class Node
{
    public int Value { get; set; }
    public Node Next { get; set; }
}

I am a complete beginner in programming. I decided to learn C# as my first programming language. I came across this code.

How is 'Node' defined as the datatype for Next? It is confusing me a lot.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tabaraq
  • 11
  • 3
  • Because a class is allowed to have an instance of itself as one of its properties. – Kevin Apr 24 '20 at 19:07
  • 2
    This looks like a standard linked-list implementation. `Next` points to the next element in the list. Imagine people standing in line and each Person is pointing to the Person next to them. – 001 Apr 24 '20 at 19:08
  • Put the cursor inside Node, hit F12 and see where it jumps to. If it's not in a `System` or `Microsoft` namespace then it;'s not part of the framework – Mateech Apr 24 '20 at 19:08

4 Answers4

1

This is a good question to ask as you're learning about C#. The key is that there are two kinds of types in C#: "value" types and "reference" types. See this question and its answers for more details.

Because Node is declared as a class, that means it is a reference type. If you make a variable with a reference type, then that variable doesn't hold the data directly; instead, it holds a reference that can point to the data. By default, references have the special value null, which means they don't point to anything. When you assign a variable e.g. myNode.Next = someOtherNode, you don't copy the entirety of someOtherNode to the Next property; you just copy a reference to someOtherNode into the property.

So by the Node class itself having a Node property, a Node object doesn't actually contain another Node object. The first object contains a reference to the second object. This allows one node to point to another node, which can then point to another node, and so on. A collection of nodes organized this way is called a linked list; in this case, it's a linked list of int (32-bit integer) values.

If Node were a value type (declared as a struct instead of a class), then there would indeed be a problem. Value type variables contain the data directly, so you cannot have an instance of a value type which contains another instance of that same value type.

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
1

Like other answers, this class represents a node for a linked list. In this case the Node can point to another instance of Node.

Node 
{
   int Value = 1;
   Node Next =============>  Node {
}                                   int Value = 2;
                                    Node Next ===========> Node {
                                  }                                int Value = 3;
                                                                   Node Next =======> null
                                                                 }

You don't usually come across classes having references to themselves like Node.

Stack Undefined
  • 1,050
  • 1
  • 14
  • 23
  • Though, I would disagree about your last sentence. They can be pretty usual, especially if you have classes that represent your standard database for your company's data. An employee has a manager, which is another employee, etc. – Pac0 Apr 24 '20 at 20:21
  • @Pac0 That's why I qualified that sentence with the word "usually". Still I think the number of classes referencing themselves like `Node` and Employee entity is far less. – Stack Undefined Apr 24 '20 at 21:28
0

This is called a linked list, an example of a recursive data structure. This can easily be instantiated in C# because the recursion can be ended by letting the last node in the list have a null value for the Next property.

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46
0

That looks like a class defining a node in a singly linked list that stores integer values. Remember a class is just a definition of the shape of an object. This may work like:

var node1 = new Node { Value=1} ; node1.Node = new Node { Value=2};

Andrew
  • 5,215
  • 1
  • 23
  • 42