in Technology by
What is Static Constructors in C#?

1 Answer

0 votes
by

Static Constructors

Static constructor is used to initialize the static data members of the class. Static constructor is only called once while creation of the first instance of the class. After that, no instance of a class will call the static constructor. You can also use static constructor to execute some code of the class which must be executed only once.

public class Person

{

    static Person()

    {

        //Static Members

    }

}

In inheritance, the calling of the constructor starts from the parent class.

Let's see how to use these constructors -

static void Main(string[] args)

{

    Person p1 = new Person();//This will call Default Constructor

    Person p2 = new Person("Pravin", "D");//This will call two parameterized Constructor

    Person p3 = new Person(p2);//This will call Copy Constructor

}

It is worth mentioning that you can also create a private constructor, which is  generally used in classes that contain static members only. If you create a private constructor, you cannot create an instance of a class.

class Person

{

    // Private Constructor:

    private Person() { }

     

    ...

}

I hope you got an idea of the different types of Constructors in C#. This question is frequently asked in interviews, so make sure to revise it before heading for your interview.

Related questions

0 votes
    What is Parameterized constructors in C#?...
asked Jan 1, 2021 in Technology by JackTerrance
0 votes
    Explain Different Types of Constructors in C#?...
asked Dec 31, 2020 in Technology by JackTerrance
0 votes
    How to print details in the pattern given in java constructors like this Write a program that would print the ... - WallsStreat ?? Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    How to print details in the pattern given in java constructors like this Write a program that would print the ... - WallsStreat ?? Select the correct answer from above options...
asked Dec 18, 2021 in Education by JackTerrance
0 votes
    What is the return type of Constructors? (a) int (b) float (c) void (d) none of the mentioned ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Which of these constructors is used to create an empty String object? (a) String() (b) String(void ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these exceptions is thrown by URL class's constructors? (a) URLNotFound (b) URLSourceNotFound (c) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What are the differences between the constructors and methods?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    Can we overload the constructors?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    How many types of constructors are used in Java?...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    How to configure static files in Django?...
asked Jul 1, 2021 in Technology by JackTerrance
0 votes
    Static code analysis is performed before the ________ begins. (1)Testing (2)Coding (3)Monitoring (4)Program execution...
asked May 19, 2021 in Technology by JackTerrance
0 votes
    Can “this” be used within a static method?...
asked Mar 31, 2021 in Technology by JackTerrance
0 votes
    How do you differentiate dynamic cache from static cache?...
asked Mar 26, 2021 in Technology by JackTerrance
0 votes
    Arrange in order- A. running unit test B. running static code check C. check for code D. Build Package...
asked Mar 7, 2021 in Technology by JackTerrance
...