如何使用Singleton类 [英] How to consume Singleton class

查看:90
本文介绍了如何使用Singleton类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是新手单身类如何调用显示方法你可以引导或发送片段





Hi All,
I am new to singleton class how to call the display method can u guide or send snippets


public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
  public string display(string msg)
  {
     return "you have entered " + msg;
   }
}

推荐答案

这个单例定义毫无意义。特别是,显示没有意义,不正确(可行,但需要 static ,因为这个未使用)与单例功能无关。公开实例会起作用,但意味着风格不好。



首先,请阅读:http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ]。



这是一个很好的单例样本:http://csharpindepth.com/Articles/General/Singleton.aspx [ ^ ]。



-SA
This singleton definition makes no sense. In particular, display makes no sense, incorrect (would work, but it needs to be static, because "this" is not used) and unrelated to singleton functionality. Exposing Instance would work but means bad style.

First, read this: http://en.wikipedia.org/wiki/Singleton_pattern[^].

And here is a good singleton sample: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA


试试Singleton.display(输入字符串)..



对于单身人士,你可以使用classname.methodname ()..



有帮助吗?
Just try Singleton.display("input string")..

For singleton, you can consume as classname.methodname()..

does this help?


这篇关于如何使用Singleton类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆