Trying to understand how static is working in this case

Viewed 108

I am looking at some code that a coworker wrote and what I expected to happen isn't. Here is the code:

public class SingletonClass
{
    private static readonly SingletonClass _instance = new SingletonClass();

    public static SingletonClass Instance
    {
        get { return _instance; }
    } 

    private SingletonClass()
    {
        //non static properties are set here
        this.connectionString = "bla"
        this.created = System.DateTime.Now;
    }
}

In another class, I would have expected to be able to do:

private SingletonClass sc = SingletonClass.Instance.Instance.Instance.Instance.Instance.Instance;

and it reference the same Instance of that class. What happens is I can only have one .Instance. Something that I did not expect. If the Instance property returns a SingletonClass class, why can't I call the Instance property on that returned class and so on and so on?

2 Answers
Related