我们可以在c#中使用类名而不是返回类型吗?

时间:2015-11-03 13:29:45

标签: c#

我们可以拥有类名而不是返回类型吗?如果是,那么如何,如果不是如何? ,请用这个基本单例模式解释我

  class Program
{
    static void Main(string[] args)
    {
        Sample s1;
        s1 = Sample.cr();
        Sample s2 = Sample.cr();
        Console.ReadLine();
    }      
}
class Sample
{
    static Sample p;
    private Sample()
    { 

    }
    // Why Here Retrun Type Is Missing I Mean Void / Int ?
    public static Sample cr()
    {
        if(p==null)
        {
            p = new Sample();

        }
        return p;
    }

}

1 个答案:

答案 0 :(得分:3)

示例中缺少 的返回类型。您的cr方法具有返回类型,它是Sample

您的任何自定义类都是类型,不仅是intstring等原始类型。

相关问题