Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i want to know that concept of constructor each and every part.

What I have tried:

i want to know that concept of constructor each and every part.
Posted
Updated 23-Jul-18 3:09am
Comments
Leo Chapiro 23-Jul-18 8:34am    
Is it a statement, a wish or a question?
Member 11621026 23-Jul-18 8:39am    
question
Richard MacCutchan 23-Jul-18 9:28am    
You can find the definitive answer by studying the documentation for the relevant programming language you are using.

A constructor allows you to prepare an instance so that it is ready to use the moment it is needed: you don't need to set up counts, or names, or whatever you class requires once the actual memory has been assigned each time you need a new one.

Having said that, you don't need an explicit constructor in order to instantiate an object - the system will create one for you that does nothing if you do not supply any constructors.

In addition, constructors allow you to specify which classes are allowed to create a new instance of the class being instantiated: if you create just a private constructor for example, they it can only be created from within the class itself, so patterns like the Singleton pattern become possible.
 
Share this answer
 
Comments
Member 11621026 23-Jul-18 8:47am    
In c# do have any live project example to clear that.in case study i have read so many time.but in live i did not apply that.
OriginalGriff 23-Jul-18 8:55am    
No, because it's not "a constructor" each class with have a different constructor for a different purpose.

Try creating a few for yourself, and see what they do instead of looking for examples you can hand in - they will make a lot more sense that way.
Constructor is used to object initialization and memory allocation of the class.
different types and there uses
Public constructor. Public constructors allow a class to be instantiated from an external location in a program. Most constructors will be in the public accessibility domain.
Private constructor
Private constructor is used in class that contains only static members. Basically private constructor prevents to create instance of the class.
Static constructor

It is used to initialize any type of static data of the class or perform action need to be performed only once. Static Constructor is called automatically before the first instance of the class or any static data is referenced. It is called only once for any number of classes instance is created.

Default Constructor

A constructor without any parameters is called default constructor. It initializes the same value of every instance of class.

Parameterized Constructor

A constructor which has at least one parameter is called Parameterized Constructor.by using parameterized constructor we can initialize each instance of the class to different values.
 
Share this answer
 
Comments
Member 11621026 25-Jul-18 0:26am    
I know the definition but i find real example to how to use this at what time i have use constructor

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900