private instance constructor

  • Thread starter Thread starter TonyJ
  • Start date Start date
T

TonyJ

Hello!

If you have private instance constructor you can then instance object from
within the class itself.
It is any point in doing this? I mean is there situation where this can be a
point doing this.

//tony
 
Hello TonyJ,

This is mostly used when you need to control the creation of your object,
for example in Singleton pattern
when we hide the constructor and manually controlling when create the new
instance and when return existed one

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


T> If you have private instance constructor you can then instance object
T> from within the class itself.
T>
 
3 common reasons:
* in 1.1, as part of a static class
* a ctor used by the public ctors
* for use from static members (such as the singleton pattern).

ctor example:

private MyClass(int value, string name) {
// do all the actual work
}
public MyClass(string value) : this(0, value) {}
public MyClass(int value) : this(value, "") {}

-------

singleton (simple field approach; other patterns are available ;-p):

private MyClass() {}
public static readonly MyClass Default = new MyClass();

-------

other static method:

private static int counter;
private MyClass(int id) {
this.id = id;
}
public static MyClass Create() {
// give each a unique id, or do something else useful ;-p
int newId = Interlocked.Increment(ref counter);
return new MyClass(newId);
// (ok, this could be done in the instance ctor, but hey!)
}
 
TonyJ said:
Hello!

If you have private instance constructor you can then instance object from
within the class itself.
It is any point in doing this? I mean is there situation where this can be a
point doing this.

How about where you want to take mostly similar but slightly different
actions on data passed when it's coming from a different constructor? e.g.:

public class A
{

public A()
: this("some","default","values",0.01m,false)
{}

public A(string a, string b, string c, decimal d)
: this(a,b,c,d,true)
{

}

private A(string a, string b, string c, decimal d, bool hidden)
{
if (hidden)
{
// Do some automatic stuff, rather than popping
// up a dialog box and asking the user.
}
else
{
// Prompt a user for some information
}

// Do stuff common to both constructors.
}
}

Chris.
 
Hi,

It's used in a lot of places.

In addition to the other posts, consider the case of a DataRow. It's
existence and its very same structure depends of a containing table. What
is the solution?
DataTable has a NewRow method that internally create a DataRow and return
it. You cannot create a DataRow on your own.

The above pattern assure that the DataRow will be correctly initialized.

There are more examples like this. just post back if you need more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top