C # Singleton improvements

xiaoxiao2021-03-06  25

Public Class Singleton

{

// private static bool instance_flag = false; instance flag is useless, directly determine if instance is instance, can solve this problem.

Private static singleleton

PUBLIC STRING NAME;

Private singleton () // Change the Singleton function to private to prevent it from calling New to create it.

{

Name = "guqi";

Console.writeline ("You can only see me once");

}

Public Static Singleton getInstance ()

{

IF (instance == null) {

// instance_flag = true;

Instance = new singleleton ();

Return Instance;

}

Else Return Instance;

}

}

Thank you VB1980 improvements to the code.

Public Static Singleton getInstance ()

{

IF (instance == null)

{

Instance = new singleleton ();

}

Return Instance;

}

Many simple.

转载请注明原文地址:https://www.9cbs.com/read-81318.html

New Post(0)