How to return an instance of a static class in C #

I would like to get an instance of a static class, but I cannot do this without implementing a singleton wrapper in 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; } } } 
+6
source share
5 answers

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

You may be confused:

 private static MyClass _myInstance; 

It just means that there will be one instance of this particular object among all objects created by an instance of a type that has _myInstance as a member.

A few notes:

  • The keyword this not valid in a static member.
  • If you have a static class, then all members must be static and therefore this will never be valid.
  • Singleton class cannot be a static class
  • Singletons declare one static member to ensure that there is only one instance of this class.
  • Note that a static reference to an object does not make the object static. Only the link is static.

Further reading: Jon Skeet has a great record for implementing Singletons in C # In Depth . I would suggest reading and studying this article until you forget it. It's not bad.

+13
source

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

You can access the class everywhere, why return an instance ? I can not imagine any reason for this.

Using a static class

To use a static class, simply write it as shown below:

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

As you can see, it does not even make sense to declare a variable, because it will look like this:

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

If you want to have a shorter name that you can simply use:

  using MSC = MyNamespace.MyStaticClass; 
+2
source

From your comments, I assume that your solution will be:

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

+1
source

Your terminology is incorrect. Please read the MSDN article for the static keyword .

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

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

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

The main problem here is:

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

this refers to an instance of a class that does not make sense in a static class, since there cannot be an instance of one. This in itself should make you realize that there is a fundamental error in what you ask: "I would like to get an instance of a static class." You cannot return an instance of a static class, since a static class by definition cannot be created.

The singleton pattern simply ensures that you always return the same instance of the class. But the specified class can never be static.

0
source

Source: https://habr.com/ru/post/915277/


All Articles