-2

What will actually happens with references when we create an instance of the class in that same class like this:

public class Class1
{
  public Class1 instatnce;
}

And how it will look references when we'll try to get access to instance.

AlexCSH
  • 3
  • 1

1 Answers1

0

Its not a problem. For example:

public class Class1
{
  public Class1 Instance {get; set;}
}

public class Class2
{
     public void Test()
     {
         Class1 class1 = new Class1();
         var nestedInstance = class1.Instance;
     }
}

Please read about singleton pattern: https://msdn.microsoft.com/en-us/library/ff650316.aspx This let you to understand this case

Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
  • A class referencing itself has nothing to do with singletons. Consider a class `Employee` which has a property `public Employee Boss{get;set;}`. You can have many employees not only one, but all have a boss which is also an employee. – Tim Schmelter May 05 '15 at 10:09
  • Singleton is good example of this operation. How this references works – Pawel Maga May 05 '15 at 10:10