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:
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
wrong code
sorry
There are no nested classes in your example.
2sec
yeah sorry 2sec
here
Do you know what a linked list is and how it works?
yep
it doesn't need "contiguous" memory like an array
Then you know the answer :p
hmm
as to specifically nested classes, we nest
Node
here because it doesnt make sense outside the context of the listfrom 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
notice how the public API of the list doesnt expose
Node
sort ofyeah, why is it important to make it private here
because it can be
Node serves no purpose on the outside
so we nest it and hide it with
private
yeah I see
but the thing is hmm, couldn't we omit the GenericList class? like only have the node class ?
no
hmm why it would break things?
... are you trolling?
ðŸ˜
no :c
Where did you get the above code from?
it was from microsoft doc but I don't remember exactly on which article
Thats essentially the "standard" linked list implementation
it's not written by me
Ok so lets pretend we only keep
Node
this is clearly not a listyep
it's just an object yeah
like person
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?ahh didn't see that
yeah, it must have several nodes that can be traversed
it must be able to have multiple nodes, yes
a list can be empty, or have a single element too
yep
will just refresh a bit just give me 2min, try to make sense of all that
You know how namespaces work, right?
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
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
.yeah,
Node
existing is just an implementation detail
so we hide it, like we would any other "helper"yeah I see, the thing is we could have done something like this:
we just omit the outer class, create a new class Node and implements everything here
it would be bad practice ?
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
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 ?
And then came partial classes, where we separate nested classes elegantly🤔 . Oh sorry different topic.