I am reading a code in C# that uses two constructors. One is static and the other is public. What is the difference between these two constructors? And for what we have to use static constructors?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
staticandpublicare orthogonal concepts (i.e. they don’t have anything to do with each other).publicsimply means that users of the class can call that constructor (as opposed to, say,private).staticmeans that the method (in this case the constructor) belongs not to an instance of a class but to the “class itself”. In particular, a static constructor is called once, automatically, when the class is used for the first time.Furthermore, a static constructor cannot be made
publicorprivatesince it cannot be called manually; it’s only called by the .NET runtime itself – so marking it aspublicwouldn’t be meaningful.