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 152 views
1 answer 169 views
1 answer 147 views
1 answer 179 views
1 answer 150 views
150 views asked Aug 22, 2018 by avibootz
1 answer 158 views
...