How to return a static class instance in c#

后端 未结 5 2007
花落未央
花落未央 2021-01-17 21:48

I would like to get an instance of a static class, but I can’t seem to do this without implementing a singleton wrapper on a non-static class– is this possible, or am I miss

5条回答
  •  鱼传尺愫
    2021-01-17 22:35

    Your terminology is wrong. Please read the MSDN article on the static keyword.

    A static member cannot be referenced through an instance. Instead, it is referenced through the type name.

    A singleton is a class that only allows a single instance of itself. A common implimentation of this in C# is:

    public class MyClass
    {
        private MyClass _value = null;
    
        public MyClass Value {
            get { return _value ?? (_value = new MyClass()); }
        }
    }
    

提交回复
热议问题