in Technology by
What is Default Constructor in C#?

1 Answer

0 votes
by

When you do not declare any constructor, the class will call its default constructor which has a default public access modifier. The default constructor is a parameter less constructor which will be called by a class object.

Let's see an example of a Default Constructor -

public class Person

{

    private int m_PersonID;

    private string m_FirstName, m_LastName, m_City;

    public Person()

    {

        m_PersonID = 19929;

        m_FirstName = "No First Name";

        m_LastName = "No Last Name";

        m_City = "No City";

    }

}

This default constructor will be executed whenever the class is initialized – Person p = new Person();

Related questions

0 votes
    What is the purpose of a default constructor?...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    What is Copy Constructor in C#?...
asked Jan 1, 2021 in Technology by JackTerrance
0 votes
    What is Default interface methods in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I am using log4net in a C# project, in the production environment, I want to disable all the logging ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I am using log4net in a C# project, in the production environment, I want to disable all the logging ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Can an abstract class have a constructor?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    What is the difference between a constructor and a method?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    If we need to inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how can we do that?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    What should be the execution order, if a class has a method, static block, instance block, and ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    I'm trying to create two dimensional std::vector, that will hold objects of this class I inherited. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    What are the best practices if you have a class which accepts some parameters but none of them are allowed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 5, 2022 in Education by JackTerrance
...