简体   繁体   中英

How to return a static class instance in c#

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 missing something?

public class MyInstanceTester
 {
    public MyInstanceTester()
    {
        //this is how i get a reference to a singleton now
        MyClass instance1 = MyClass.Instance();
        //this is what is would like to do (if only the compiler would let me)
        MyStaticClass instance2 = MyStaticClass.Instance();
    }
}


public class MyClass
{
    private static MyClass _myInstance;

    static MyClass()
    {
        _myInstance = new MyClass();
    }


    public static MyClass Instance()
    {
        return _myInstance;
    }

}

public static class MyStaticClass
{
    public static MyStaticClass Instance
    {
        get
        {
            return this;
        }
    }
}

There is no such thing as an instance of a static class. The singleton pattern simply returns the same instance of a class to repeated requests.

You may be getting confused by:

private static MyClass _myInstance;

This simply means that there will be a single instance of that particular object among all objects instantiated of the type that have _myInstance as a member.

A few notes:

  • The this keyword is not valid in a static member
  • If you have a static class then all members have to be static and so this will never be valid
  • A Singleton class cannot be a static class
  • Singletons declare a single static member to help ensure that only a single instance of that class exists
  • Note that a static reference to an object does not make the object static. Only the reference is static

Further reading: Jon Skeet has an excellent write up on implemeting Singletons in C# In Depth . I would suggest reading and studying this article until you grok it. It is quite good.

There is no reason to return a instance to a static class ( If the class is static there is no instance ).

You can access the class from everywhere, why returning a instance to it? I can't imagine any reason to do this.

Static class usage

To use a static class just write it like below:

MyStaticClass.MyMethod();
Int32 callCount = MyStaticClass.CallCount;

As you can see it doesn't even make sense to declare a variable because this would just look like this:

MyStaticClass msc = MyStaticClass.Instance();
msc.MyMethod();
Int32 callCount = msc.CallCount;

If you want to have a shorter name you just can use:

 using MSC = MyNamespace.MyStaticClass;

From your comments I assume your solution would be:

Make your class non-static. (Just keep the methods static.)

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()); }
    }
}

The major problem is here:

public static class MyStaticClass 
{     
    public static MyStaticClass Instance
    {         
         get
             {             
                  return this; //compile time error!
             }
    }
}

this refers to an instance of a class which does not make sense in a static class as there can be no instance of one. This by itself should make you realize that there is a fundamental error in what you are asking: "I would like to get an instance of a static class". You can not return an instance of a static class as a static class by definition can not be instantiated.

The singleton pattern just makes sure that you always return the same instance of a class. But said class can never be static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM