You must Sign In to post a response.
  • Category: .NET

    Can we use use n number of constructors within class by using same class name

    is it possible to use n number of constructors within class by using same class name
  • #746490
    Hai Abhinav,
    yes, we can have as many constructors with different parameter. The Constructor supports polymorphism behavior.
    So constructor can be overloaded in the same class it defined.
    e.g.

    class Customer
    {
    public Customer(){} // default constructor
    public Customer(int custid)
    {
    // some implementation
    }
    public Customer(string custName, string location)
    {
    // some implementation
    }
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #746514
    You can have N number of constructor for a class but they must be different in terms of parameters you pass in it.

    No 2 constructor can have a common signature.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #746522
    Hi abhinav,

    Constructor is also same like a method but the thing is that method name is same like a class name. In method you can able to define same name with different signature ( method overloading) right..? In the same passion you can able to create N no.of constructors with different signature.

    Hope this will help you to clear your doubt...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #746530
    Hi,
    You can have as many constructiors in your class with different signature like below,

    public A(){}
    public A(int i)
    public A(int i, int j)
    public A(int i, int j, int k)


    Let me know if you have any doubts in this regard.


    Regards,
    Asheej T K

  • #760064
    Hi

    We can have n number of constructors for a class which is also called as constructor overloading.
    Constructor can be overloaded i.e., same name but with different signatures.

    If we dont provide any constructor manually by default every class contains a constructor.
    If we write manually a constructor then default constructor is not provided by clr.

    Constructor:
    *It is a method executed automatically when we
    create an object.
    *Constructor name must be same as class name.
    *Constructors are overloadable and does not have
    return type.

    Regards

    Sridhar Thota.
    DNS Member.
    "Hope for the best.. Prepare for the worst.."

    Sridhar Thota.
    Editor: DNS Forum.

  • #760109
    Hi,

    constructor is nothing but a Special methods which is present in each and every class which is responsible for initialize the variable of a class..

    its same like class and can be overload and override that means we can have n no of constructor in side the class..

    Thnaks,
    Chitaranjan


  • Sign In to post your comments