Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a Class as a Member of itself

Tags:

I was working in the Microsoft.Ink dll recently using C# and was debugging a problem (which is not related to this) I noticed, that when I was debugging it, ink objects had a strokes object, which had an ink object, which had.... etc.

This confused me, as I was under the assumption you could not do this (I come from a C++ Background)

But I ignored it, solved the problem, and moved on. Today, I run into a similar problem, as I look at a class which had a private member which was the same class as itself.

public sealed class Factory
{

    private static Factory instance = new Factory();
}

How is that even possible? I can now call instance.instance.instance.instance...etc. This, as you can imagine, hurts my mortal brain, and I'm sure it can't be good on the computer either. How does the compiler deal with this? And Just how deep does the rabbit hole go?