I was wondering if it was possible to create 2 constructors inside the same class. I looked up some information and found out that there is a keyword "THIS" that i can use, BUT i wanted to know if it was possible to create 2 constructors in the same class WITHOUT using "THIS".
like for example would i be able to create :
class myClass
{
public constructor_1()
{
//code
}
public constructor_2()
{
//some more code
}
}
class Main()
{
//code
}
Loading

Veena SardaRank #79 · 24.5K reputationPosted Oct 6, 2013, 8:04 PM EDT
Puneet GuptaRank #2,006 · 724 reputationPosted Oct 6, 2013, 8:18 PM EDT
http://www.codeproject.com/Articles/7011/An-Intro-to-Constructors-in-C
Thanks,
Puneet Gupta
VulpesRank #55 · 29.2K reputationPosted Oct 6, 2013, 11:13 PM EDT
Incidentally, the 'this' keyword is used when you want to 'chain' constructors together i.e. call one from another. For example:
class MeaningOfLife
{
public MeaningOfLife() : this (42)
{
// default constructor, passes 42 to other one which does the work
}
public MeaningOfLife(int i)
{
// code
}
}