Constructor Overload

Posted by Ray.chayan under C# on 6/26/2010 | Views : 1466 | Status : [Member] | Replies : 1
Hi I was doing some R&D on Constructor of C# class . I found that when we use a public and a private constructor in the same class I must overload them . i.e the signature must be different. But when I place a static constructor I can place it with the same signature . The sample code is given bellow . Please tell me how it is possible.
class C1
{
static Double pi;
public C1()
{
Console.WriteLine("Constructor :" + "C1");
}
C1(string s)
{
Console.WriteLine("Private Constructor :" + s);
}
static C1()
{
pi = 3.14;
}
public void m1()
{
Console.WriteLine("Method :" + "M1 " + pi.Tostring() );
}
}
class Program
{
static void Main(string[] args)
{
C1 c = new C1();
c.m1();
Console.ReadLine();
}
}





Responses

Posted by: Neeks on: 7/16/2010 [Member] Bronze

Up
0
Down
As you have declared C1 as constructor and static C1 as method so it will work

But if you are trying to do this with method it won't work it will give you error

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response