Declaring a class as a member

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

This confused me, since I was on the assumption that you could not do this (I come from the background in C ++)

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

public sealed class Factory
{

    private static Factory instance = new Factory();
}

How is this possible? Now I can call instance.instance.instance.instance ... etc. This, as you can imagine, harms my death brain, and I’m sure that it cannot be good on a computer either. How does the compiler handle this? And how deep is the rabbit hole?

+3
source share
9 answers

Because it is static and, therefore, inside the AppDomain there is only one copy of the variable instance.

What do you think about this:

public class Foo
{
  private Foo lol = new Foo();
}

Note that everything here is an instance, not a static one.

(), , StackOverflowException, , . , , .

OP, , static, . AppDomain, "" . , ( OP) .

+7

, Singleton".

, , - .NET Framework. Singleton ( ) , . , .

+6

. . .

class Directory
{
   string name;
   Directory parentDirectory;
}

.

+5

, , , , . , .

, , , , . , Factory, . instance.instance.instance , . - Factory.instance.

, ~ ~ - Factory, . instance.instance.instance - , . , .

+5

"", . - Factory.instance.

string text = Factory.instance.ToString(); // legal
string text2 = Factory.instance.instance.ToString(); // compiler error
+2

, -: ? Factory - , , .

, , Factory , . , (, , , ). ChainedListItem , , . () , _nextItem:

public class ChainedListItem<T>
{
    private ChainedListItem<T> _nextItem;
    T _content;

    public ChainedListItem<T> NextItem
    {
        get { return _nextItem; }
        set { _nextItem = value; }
    }

    public T Content
    {
        get { return _content; }
        set { _content = value; }
    }

    public ChainedListItem<T> Add(T content)
    {
        _nextItem = new ChainedListItem<T>();
        _nextItem.Content = content;
        return _nextItem;
    }

    public void Dump()
    {
        ChainedListItem<T> current = this;
        while ((current = current.NextItem) != null)
        {
            Console.WriteLine(current._content);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        ChainedListItem<int> chainedList = new ChainedListItem<int>();
        chainedList.Add(1).Add(2).Add(3);
        chainedList.Dump();        
    }
}

" " , . , stackoverflow, .

, , , Singleton, () , .

, , ++.

+1

singleton. , .

? , # ,

public class SomeClass
{

  static readonly SomeClass instance = new SomeClass();


        public static SomeClass Instance
        {
            get { return instance; }
        } 


        static SomeClass() 
        {
        }

        SomeClass()
        {
        }
}
0

, , . , Singleton, , , .

0
source

This is done all the time - this is most OO languages. instance is a static member of Factory. There is nothing unusual in this code. This is a standard Factory template. Do you also have a problem with code like this?

x = x + 1;
-2
source

Source: https://habr.com/ru/post/1709804/


All Articles