How to use private constructor in C#

1 Answer

0 votes
using System;

public class Test {
    private Test() { }
    public static int n;
    public static int addOne() {
        return ++n;
    }
}


class Program
{
    static void Main() {
        Test.n = 45;
        Console.WriteLine(Test.n);
        
        Test.addOne();
        Console.WriteLine(Test.n);
    }
}



/*
run:

45
46

*/

 



answered Sep 9, 2020 by avibootz

Related questions

1 answer 255 views
1 answer 265 views
1 answer 251 views
1 answer 290 views
1 answer 266 views
266 views asked Aug 22, 2018 by avibootz
1 answer 257 views
...