An example to show overloaded constructors.
Example:
using System;
class MethodOverload
{
//Observation: constructor has same name as the class
MethodOverload(string str)
{
Console.WriteLine("Display from one parameterized constructor: " , str);
}
MethodOverload(string str1,string str2)
{
Console.WriteLine("Display from two parameterized constructor: {0} {1}", str1, str2);
}
MethodOverload(string str1, string str2,string str3)
{
Console.WriteLine("Display from three parameterized constructor:: {0} {1} {2}", str1, str2 , str3);
}
static void Main(string[] args)
{
// it calls first parameterized constructor
MethodOverload obj1 = new MethodOverload("Hello");
// it calls second parameterized constructor
MethodOverload obj2 = new MethodOverload("hey","hello");
// it calls third parameterized constructor
MethodOverload obj3 = new MethodOverload("hey","hello","you!");
Console.ReadLine();
}
}
Output:
Display from one parameterized constructor: Hello
Display from two parameterized constructor: hey hello
Display from three parameterized constructor: hey hello you!
