Sathyaish <Sa*******@Yahoo.com> wrote:
How does a constructor of one class call another of the same class?
See http://www.pobox.com/~skeet/csharp/constructors.html
When would this mechanism make sense or be required?
PS: Two instances I saw are:
(a) While using the Singleton pattern
(b) While using the Factory pattern
I wouldn't expect either of these to use this pattern. I'd expect both
of them to have static methods/properties/initialisers which called the
constructor, but there's no need to have two constructors there.
It makes sense to have multiple constructors when there are several
parameters, some of which can take defaults. For instance:
public Foo() : this (0, 0)
{
}
public Foo (int x, int y)
{
this.x = x;
this.y = y;
}
allows defaulting of x and y to 0 by overloading. Usually in this kind
of situation all constructors end up calling the most specific one,
passing in default values.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too