-2

我创建了一个单例类,当我在启动 Visual Studio 后第一次执行它时,它会打印预期的结果,因为 count 的值最初为零,当它达到一个时它会退出循环,但是,当我第二次执行它,计数器值仍然保持为 1,即使在我停止调试后它也不会归零。请帮助我找到问题的解决方案。谢谢。我的班级代码如下:

public partial class Singleton_class : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CEO c1 = CEO.GetCeoObject("Rahul", "MS", 28);
        CEO c2 = CEO.GetCeoObject("Ram", "MS", 26);
        Response.Write(c1.name + " " + c1.qualifiaction + " " + c1.age + "<br/>");
        Response.Write(c2.name + " " + c2.qualifiaction + " " + c2.age + "<br/>");
     }
}
namespace Singleton
{
    public class CEO
    {
        public static CEO c1;
        public static int count;
        public string name;
        public string qualifiaction;
        public int age;

        private CEO(string n, string q, int a)
        {
            this.name = n;
            this.qualifiaction = q;
            this.age = a;
        }
        public static CEO GetCeoObject(string name, string quali, int age)
        {
            if (count == 0) //this remains at one
            {
                c1 = new CEO(name, quali, age);
                count++;
            }
            return c1;
        }
    }
}
4

2 回答 2

5

每次附加调试器时,IIS 不会重新启动 AppPool,如果要重置计数器,则必须重新构建解决方案或触摸 web.config 文件以强制 IIS 重新启动 AppPool(您的静态变量将保留到AppPool 回收)。

单例模式是一种确保在整个应用程序中只有一个对象实例的方法,因此在类上有一个私有构造函数并确保实例化实例的调用是线程安全的很重要。

以下是如何在 C# 中实现单例的两个示例:

public class Singleton
{
    private static readonly Singleton SingleInstance;

    static Singleton()
    {
        /* initializes the static field the first time anything is accessed on 
           the Singleton class .NET ensures threadsafety on static initializers. */
        SingleInstance = new Singleton(Datetime.Now);
    }

    public static Singleton Instance
    {
        get
        {
            return SingleInstance;
        }
    }
    /// <summary>
    /// Keep the constructure private on Singleton objects to avoid other instances being contructed
    /// </summary>
    private Singleton(DateTime value)
    {
        Value = value;
    }

    public DateTime Value { get; private set; }
}

如果您使用的是 .NET 4 或更高版本,您还可以使用 Lazy 来进一步简化单例

public class Singleton
{
    private static readonly Lazy<Singleton> LazyInstance = new Lazy<Singleton>(() => new Singleton(DateTime.Now));

    public static Singleton Instance
    {
        get
        {
            return LazyInstance.Value;
        }
    }
    /// <summary>
    /// Keep the constructure private on Singleton objects to avoid other instances being contructed
    /// </summary>
    private Singleton(DateTime value)
    {
        Value = value;
    }

    public DateTime Value { get; private set; }
}
于 2013-06-19T23:02:30.607 回答
4

GetCeoObjectc1仅在第一次调用时创建内部静态对象。
当您第二次调用它时,GetCeoObject 返回c1第一次调用中创建的值。
您将它分配给不同的变量,但这与c1在第一个变量中分配的实例的引用相同。它是对同一内存区域的引用。
当您打印这些值时,它会打印相同的值也就不足为奇了。

于 2013-06-19T22:58:49.230 回答