Is it correct for a C# Class to reference the same class?

Let us start by the line that you understand:

public string Name;
//  ^    ^     ^
//  |    |     |
//  |    |     +-- Name of the field
//  |    +-------- Type of the field
//  +------------- Accessibility of the field

So, we have field named Name which is of type string and it is public.

Now…

public Item Parent;
//  ^    ^     ^
//  |    |     |
//  |    |     +-- Name of the field
//  |    +-------- Type of the field
//  +------------- Accessibility of the field

We have field named Parent which is of type Item and it is public.

which has the same name as a class?

No, it does not. It has the same type as the class. Which is perfectly valid, beause a class is a reference type.

What it is saying is that an Item can have a Parent which would also be an Item.

That is, an Item has one or none Parents, we also infer that it has none, one or multiple childs. We could take it a hierarchical structure with navigability going from the leaves to the roots (there could be multiple roots). However, there is nothing preventing loops. And, yes, we could also take it as a linked list.

Leave a Comment