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;
}
}
}
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.
Static methods can access the static variables and static methods directly. Static methods can't access instance methods and instance variables directly. They must use reference to object. And static method can't use this keyword as there is no instance for 'this' to refer to.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor.
Whenever you return values from a static method they are either static nor instance by default, they are just values. The user invoking the method can use them as he wants. i.e. you can retrieve the values and declare them static.
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:
this
keyword is not valid in a static memberthis
will never be validFurther 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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With