C # Static class single case mode comparison

The company’s classes need to be implemented in a singleton mode, which can save resources and avoid the generation of duplicate objects. But the static class can also do this, and it is more concise to write, so I checked the relevant information, hoping to understand the difference between the two.

1. Singleton mode can be initialized when it is used, and static classes are initialized when the program starts to run?

Through my own practice, I think this statement is wrong. I wrote the following example to experiment.

 class Program

{
static void Main(string[] args)
{

Console.WriteLine(Class1.
get(190< span style="color: #000000;">));
GC.Collect();
Console.ReadKey();
}
}

 public static class Class1

{
public static int[] arr = new int[20480000];

static Class1()
{
for (int i = 1; i )
{
arr[i] = i;
}
}

public static int get(int i)
{
return arr[i];
}
}

When the program enters the main function, the memory looks like this

share Picture

Obviously the arr in the static class has not been initialized, continue to execute

< img alt="Share a picture" src="/wp-content/uploads/images/language/csharp/1626812577048.png" >

Share a picture share picture

You can see that the arr of the static class is initialized at this time, and when the arr value is all 0, the memory has not changed. Could it be that Microsoft has optimized it?

2. The first sentence of GOF’s explanation of the single item is “if only instance is needed”?

Um, isn’t the static class the only instance?

3. Singleton mode, decoupling?

Share a picture

public interface IHelloWorld

{
string HelloWorld();
}

//Then two implementations of the interface.
public class HelloWorldEnglish: IHelloWorld
{
string IHelloWorld.HelloWorld()
{
return "Hello World !";
}
}
public class HelloWorldChinese: IHelloWorld
{
string IHelloWorld.HelloWorld()
{
return "Hello world!";
}
}

public class People
{
private readonly IHelloWorld peopleHelloWorld;
public People(IHelloWorld helloWorld)
{
this.peopleHelloWorld = CreateSingle()
//Here you can use singleton mode to create HelloWorldEnglish or HelloWorldChinese
}
public void SayHello()
{
Console.WriteLine(
this.peopleHelloWorld.HelloWorld());
}
}

Share a picture< /p>

After reading this code, I didn’t understand the relationship between decoupling and singleton, but I understood that interface decoupling is good (manually laugh and cry). The following heroes have the same views as me, but still no solution. Open my doubts

share pictures

4. Singletons can inherit and implement interfaces, while static classes cannot

I agree with this point of view. If you want to use singletons and want to loosely couple them, use the singleton pattern.

Static classes are more suitable for tool classes.


Reference link:

https://bbs.csdn.net/topics/390487790

https://www.cnblogs.com/ zhangzt/p/4350556.html

https://bbs.csdn.net/topics/390825682

https://www.xin3721.com/ArticlecSharp/c13696.html< /p>

 class Program

{
static void Main(string[] args)
{

Console.WriteLine(Class1.
get(190< span style="color: #000000;">));
GC.Collect();
Console.ReadKey();
}
}

 public static class Class1

{
public static int[] arr = new int[20480000];

static Class1()
{
for (int i = 1; i )
{
arr[i] = i;
}
}

public static int get(int i)
{
return arr[i];
}
}

public interface IHelloWorld

{
string HelloWorld();
}

//Then two implementations of the interface.
public class HelloWorldEnglish: IHelloWorld
{
string IHelloWorld.HelloWorld()
{
return "Hello World !";
}
}
public class HelloWorldChinese: IHelloWorld
{
string IHelloWorld.HelloWorld()
{
return "Hello world!";
}
}

public class People
{
private readonly IHelloWorld peopleHelloWorld;
public People(IHelloWorld helloWorld)
{
this.peopleHelloWorld = CreateSingle()
//Here you can use singleton mode to create HelloWorldEnglish or HelloWorldChinese
}
public void SayHello()
{
Console.WriteLine(
this.peopleHelloWorld.HelloWorld());
}
}

Leave a Comment

Your email address will not be published.