Invoke an Overloaded Constructor through this keyword (What's the Different between this two Sample code?)
Posted
by
Alireza Dehqani
on Stack Overflow
See other posts from Stack Overflow
or by Alireza Dehqani
Published on 2012-10-02T15:36:08Z
Indexed on
2012/10/02
15:37 UTC
Read the original article
Hit count: 324
using System; namespace ConsoleApplication1 { class Program { static void Main() { // Sample Sample ob = new Sample(); // line1 // Output for line1 /* * Sample(int i) * Sample() / // Sample2 Sample2 ob2 = new Sample2(); // line2 // Output for line2 / * Sample2() */
}
}
class Sample
{
// Fields
private int a;
// Constructors
public Sample(int i) // Main Constructor
{
Console.WriteLine(" Sample(int i)");
a = i;
}
// Default Constructor
public Sample()
: this(0)
{
Console.WriteLine(" Sample()");
}
}
class Sample2
{
// fields
private int a;
// Constructors
public Sample2(int i)
{
Console.WriteLine("Sample2(int i)");
a = i;
}
public Sample2()
{
Console.WriteLine("Sample2()");
a = 0;
}
}
}
© Stack Overflow or respective owner