Search code examples
c#staticgetter-setter

What does this line with auto-implemented properties with a static class mean?


I'm following the instructions for testing an azure function here and I came across this line of code:

public static NullScope Instance { get; } = new NullScope();

I've read this SO answer, so I understand auto-implemented properties, but I don't know how it combines with the static keyword.

Is this just creating a new Nullscope that you can only access and not set? Or does this create a new NullScope every time you get it? If possible, could you expand the line of code for better understanding?


Solution

  • Is this just creating a new Nullscope that you can only access and not set?

    Yes, exactly.

    Or does this create a new NullScope every time you get it?

    No, that would look more like this:

    public static NullScope Instance
    {
        get => new NullScope();
    }
    

    Notice that the former has a standard auto-implemented getter and uses some relatively recent syntax to set a value to the auto-implemented backing member, whereas the latter uses a custom getter which is just an "expression bodied member" which, when invoked, returns an object.