C
C#•3w ago
Faker

Purpose and use of nested classes in C#

Hello guys, sorry to disturb you all; can someone explain the idea of using nested classes in C# please. For example, consider the following code:
C#
// Type parameter T in angle brackets.
public class GenericList<T>
{
// The nested class is also generic, and
// holds a data item of type T.
private class Node(T t)
{
// T as property type.
public T Data { get; set; } = t;

public Node? Next { get; set; }
}

// First item in the linked list
private Node? head;

// T as parameter type.
public void AddHead(T t)
{
Node n = new(t);
n.Next = head;
head = n;
}

// T in method return type.
public IEnumerator<T> GetEnumerator()
{
Node? current = head;

while (current is not null)
{
yield return current.Data;
current = current.Next;
}
}
}
C#
// Type parameter T in angle brackets.
public class GenericList<T>
{
// The nested class is also generic, and
// holds a data item of type T.
private class Node(T t)
{
// T as property type.
public T Data { get; set; } = t;

public Node? Next { get; set; }
}

// First item in the linked list
private Node? head;

// T as parameter type.
public void AddHead(T t)
{
Node n = new(t);
n.Next = head;
head = n;
}

// T in method return type.
public IEnumerator<T> GetEnumerator()
{
Node? current = head;

while (current is not null)
{
yield return current.Data;
current = current.Next;
}
}
}
I want to implement a linkedList by scratch in C#. Why not just use a class of "Node" or "LinkedList" ? why we should have both, what if we don't have them?
35 Replies
Faker
FakerOP•3w ago
wrong code sorry
Pobiega
Pobiega•3w ago
There are no nested classes in your example.
Faker
FakerOP•3w ago
2sec yeah sorry 2sec here
Pobiega
Pobiega•3w ago
Do you know what a linked list is and how it works?
Faker
FakerOP•3w ago
yep it doesn't need "contiguous" memory like an array
Pobiega
Pobiega•3w ago
Then you know the answer :p
Faker
FakerOP•3w ago
hmm
Pobiega
Pobiega•3w ago
as to specifically nested classes, we nest Node here because it doesnt make sense outside the context of the list
Faker
FakerOP•3w ago
from my understanding, a linked list is a connection of nodes, that is we are modelling the collection as an object (kind of an "Array" of nodes), then the nodes is what contains the data/pointers
Pobiega
Pobiega•3w ago
notice how the public API of the list doesnt expose Node sort of
Faker
FakerOP•3w ago
yeah, why is it important to make it private here
Pobiega
Pobiega•3w ago
because it can be Node serves no purpose on the outside so we nest it and hide it with private
Faker
FakerOP•3w ago
yeah I see but the thing is hmm, couldn't we omit the GenericList class? like only have the node class ?
Pobiega
Pobiega•3w ago
no
Faker
FakerOP•3w ago
hmm why it would break things?
Pobiega
Pobiega•3w ago
... are you trolling?
Faker
FakerOP•3w ago
😭 no :c
Pobiega
Pobiega•3w ago
Where did you get the above code from?
Faker
FakerOP•3w ago
it was from microsoft doc but I don't remember exactly on which article
Pobiega
Pobiega•3w ago
Thats essentially the "standard" linked list implementation
Faker
FakerOP•3w ago
it's not written by me
Pobiega
Pobiega•3w ago
Ok so lets pretend we only keep Node
public class Node(T t)
{
// T as property type.
public T Data { get; set; } = t;

public Node? Next { get; set; }
}
public class Node(T t)
{
// T as property type.
public T Data { get; set; } = t;

public Node? Next { get; set; }
}
this is clearly not a list
Faker
FakerOP•3w ago
yep it's just an object yeah like person
Pobiega
Pobiega•3w ago
not like person but yeah, its just an object it doesnt have an Add method. It cant be iterated over. its just a class. what makes a list a list?
Faker
FakerOP•3w ago
ahh didn't see that yeah, it must have several nodes that can be traversed
Pobiega
Pobiega•3w ago
it must be able to have multiple nodes, yes a list can be empty, or have a single element too
Faker
FakerOP•3w ago
yep will just refresh a bit just give me 2min, try to make sense of all that
Thinker
Thinker•3w ago
You know how namespaces work, right?
Faker
FakerOP•3w ago
Hmm I know it organises our projects I think or I'm interchanging it with solution 2sec yeah I think both are use to organise things
Thinker
Thinker•3w ago
Yeah Again you can think of namespaces are 'folders' you put classes into, purely for organizational purposes Nested classes are pretty much the same. When we nest a class inside another class, it's usually because (as Pobiega said previously) we want to group that nested class together with the other class. So for GenericList here (which again is a linked list), we put Node inside of it because (again, as previously said) it doesn't make sense to use outside of GenericList.
Pobiega
Pobiega•3w ago
yeah, Node existing is just an implementation detail so we hide it, like we would any other "helper"
Faker
FakerOP•3w ago
yeah I see, the thing is we could have done something like this:
C#
public class Node<T>
{
public T Data { get; set; }
public Node<T>? Next { get; set; }

public Node(T data)
{
Data = data;
}

// Adding a node after the current node
public void AddAfter(T newData)
{
Node<T> newNode = new Node<T>(newData);
newNode.Next = this.Next;
this.Next = newNode;
}

// Display the list starting from this node
public void Display()
{
Node<T>? current = this;
while (current != null)
{
Console.WriteLine(current.Data);
current = current.Next;
}
}
}
C#
public class Node<T>
{
public T Data { get; set; }
public Node<T>? Next { get; set; }

public Node(T data)
{
Data = data;
}

// Adding a node after the current node
public void AddAfter(T newData)
{
Node<T> newNode = new Node<T>(newData);
newNode.Next = this.Next;
this.Next = newNode;
}

// Display the list starting from this node
public void Display()
{
Node<T>? current = this;
while (current != null)
{
Console.WriteLine(current.Data);
current = current.Next;
}
}
}
we just omit the outer class, create a new class Node and implements everything here it would be bad practice ?
Pobiega
Pobiega•3w ago
sure you could do that, but its... weird like, if you keep a reference to the first node, you are continously adding to the second place of the link and the "list" itself has data, since its all a node like, sure you CAN do this, but I see no reason why you would
Faker
FakerOP•3w ago
Yeah I see like we divide the idea into 2 entities; node implies holding data and reference, while GenericList to handle the linkedlist, like adding new node etc... just read about Single Responsibility Princile (SRP)... the thing is we should always try to implement a class that adheres to a specific function ? Like in this case, GenericList would have the task of managing the list, while the class of Node would have the task of defining the data contained into the node ?
glhays
glhays•3w ago
And then came partial classes, where we separate nested classes elegantly🤔 . Oh sorry different topic.

Did you find this page helpful?