0

I am new to OOP and I am just starting my adventure with C#. I recently came across a topic on StackOverflow, where somebody asks how is it even possible to create a instance of a class inside of the class itself.

public class My_Class
 {
      My_Class new_class= new My_Class();
 }

My question is why would we ever want to do something like this? I tried to do a bit of searching on the forum but most of the discussions seem to be about how does this not create an infinite loop etc.

A simple example would be much appreciated (or perhaps a suggestion on what phrase to google)

Szmal
  • 9
  • 1

5 Answers5

1

Ok, a simple example:

class Human 
{ 
    Human Father { get; set; } 
    Human Mother { get; set; } 
}

another one:

class Employee
{
    Employee Manager { get; set; }
}

You confuse a type with instances of that type. Instances can have different meanings.

Another famous example are singletons which are used to store one instance of the type which will not be exposed and that is used in all static methods or properties.

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • The singleton concept made things a bit more clear for me. I have done some additional reading and this makes sense. I am however still a bit confused with the first two examples you provided. Could you please expand the example with the 'Human' class? Perhaps if it had some methods it would be easier to understand. – Szmal Nov 03 '17 at 13:38
  • @Szmal: what confuses you? A human has many properties, two of them are their father and their mother(every human has). Those are also humans with a father and a mother. A human class could also have a `List Children` as property which is empty if this human doesn't have children. An Employee can have a manager which is also a Employee(with a manager if it's not the CEO, otherwise `null`). – Tim Schmelter Nov 03 '17 at 13:41
  • Oh, ok, I get it now. Seems logic. Don't know where the confusion came from... Thanks! – Szmal Nov 03 '17 at 13:52
0

One example where you want to have an instance inside of the same class is in linked list structure. Each node has an instance to the previous and the next one:

public class Node 
{
    Node next;
    Node previous;

    //...
}
Héctor
  • 19,875
  • 26
  • 98
  • 200
0

Most likely, this is describing the Singleton pattern, where your program only ever has one instance of the class. In this case you tend to instantiate a single instance of the class itself from within that class.

// naive implementation of singleton, but demostrates the basics
public class Singleton
{
    private static Singleton instance;
    static Singleton()
    {
         this.instance = new Singleton();
    }

    public static Singleton Instance{ get { return instance; } }
    private Singleton() {}

    public void DoSomething() { ... }
}

usage

Singleton.Instance.DoSomething();
Jamiec
  • 118,012
  • 12
  • 125
  • 175
0

You mostly do this when implementing (variations of) the Singleton design pattern. But there is obviously a bit more code involved than what you are posting in the question.

Furthermore; no this will not create an infinite loop. It will create a StackOverflowException though, if you unconditionally construct a new instance of a class in the constructor of same class. Or as the default value for a field for that matter.

pyrocumulus
  • 8,296
  • 2
  • 37
  • 52
0

Many answers have already answered the "how" part of your question, so I will only answer the "why" in as far as it relates to the singleton design pattern mentioned by most answers:

My question is why would we ever want to do something like this?

Sometimes it's important to have only one instance for a class. For example, in a system there should be only one window manager (or only a file system or only a print spooler). Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves. - source

Tshilidzi Mudau
  • 5,472
  • 6
  • 32
  • 44