Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
can anybody tell me under what situation the constructor overloading can be used?????
Posted

Constructor is nothing just a method in a class which called automatically when an instance/object will create of the class...

Constructor can not be called withcout creating object of that class..

Generally Constructor are used to set initial values of class members while creating object of that class.

overloading means you can create one or more then one Constructor in a class but remember :

:- parameter datatype must be diffrent if u r creating more then one Constructor with same number of parameter.

:- parameter sequence must be diffrent if u r creating more then one Constructor with same number of parameter.

:- Number of parameter must be diffrent if u r creating more then one Constructor.

Sample Code :

C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Class1
/// </summary>
public class MyClass
{
    // DEFAULT CONSTRUCTOR
    public MyClass()
    {
        
    }

    // PARAMETERISED CONSTRUCTOR --> STRING
    public MyClass(string value1, string value2)
    {

    }

    // PARAMETERISED CONSTRUCTOR --> STRING
    public MyClass(string value1, string value2, string value2)
    {

    }

    public MyClass(int value1, int value2)
    {

    }

    public MyClass(string value1, int value2)
    {

    }

    public MyClass(int value1, string value2)
    {

    }
}
 
Share this answer
 
Comments
Innocent910 22-Aug-13 2:33am    
your ans is nice...
suppose I have a program to build but how i came to know that this program using overloadng or not.... ???
[no name] 22-Aug-13 3:33am    
simple and easy +5
Dineshshp 22-Aug-13 3:36am    
Enjoy the programming... always remember don't think that it is your job or work... always remember it's only a fun to become a good and happy programmer...

Because programmers who think it work or job never happy...

DinshT
[no name] 22-Aug-13 4:17am    
yes right
Refer this link

An Intro to Constructors in C#[^]

It states that:
Quote:
C# supports overloading of constructors, that means, we can have constructors with different sets of parameters. So, our class can be like this:

C#
public class mySampleClass
{
    public mySampleClass()
    {
        // This is the no parameter constructor method.
        // First Constructor
    }

    public mySampleClass(int Age)
    {
        // This is the constructor with one parameter.
        // Second Constructor

    }

    public mySampleClass(int Age, string Name)
    {
        // This is the constructor with two parameters.
        // Third Constructor
    }

    // rest of the class members goes here.
}

Quote:
Well, note here that call to the constructor now depends on the way you instantiate the object. For example:

mySampleClass obj = new mySampleClass()
mySampleClass obj = new mySampleClass(12)

I think it is now clear for you.

Regards..:)
 
Share this answer
 
Tell what? When you need more than one constructor.

Let me tell you that "overload" is a fictional term which often cause confusion. This language feature does not really deserve a special name. I suspect if no name existed, there would be much less confusion. It simply means that different methods can have the same name, so the compiler has to resolve which one to call by actual parameter passed, in cases when it's possible. This includes constructors, which are, by syntax, all have the same name as their class or structure name.

I hope you don't need explanation why a class or structure may need more than one method ("overloaded" or not). Why the same question should be asked about constructors? There are also methods.

—SA
 
Share this answer
 
v2
Refer to this article Constructor Overloading[^]
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900