overload constructor

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hi

I am working through a learning module privately where there is discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in a
class?

Thanks

doug
 
Doug said:
I am working through a learning module privately where there is discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in a
class?

Sure - often you don't want to specify the same parameters every time
you construct an object. For instance, StreamWriter has several
overloads - some take a filename, some take a Stream.

Alternatively, look at ArrayList - you don't always want to specify the
initial capacity, but sometimes you do. Removing either of those
constructors would make life harder or less efficient.

Does that help?

Jon
 
Thanks Jon

So the purpose of the constructor is to create an instance of an object and
therefore allow access to methods that are contained within that object,
And overloading would allow different parameters to be provided to the
method of different instances of that class??
 
Hi Doug,
And overloading would allow different parameters to be provided to the
method of different instances of that class??
This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi
 
Thanks Thi

Being still at the learning gate of this language I like to get clarity
about the various elements. Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

Do you know of any website links that cover these concepts? I struggled
with 'state' for a while because the material that I was reading assumed it
was obvious - maybe not to me.

Thanks again to you and Jon.

Doug
 
gordon said:
So the purpose of the constructor is to create an instance of an object and
therefore allow access to methods that are contained within that object,
Yes.

And overloading would allow different parameters to be provided to the
method of different instances of that class??

No - the parameters which are provided to the method come later, and
are independent of what you've provided in the constructor. Usually the
different parameters in the constructor allow the object to be created
with different "state" information.
 
Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.
They are object-oriented concepts. You'll be familiar with them soon,
don't worry. Just try some tutorials.
 

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