Hi...
Why are we use constructor in c# how can we create object?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jun 11, 2014, 9:11 AM EDT
However, 'new' is always used in conjunction with an instance constructor which tells it what type of object to create and may contain initialization code for that object. The first thing an instance constructor does is to call its base class constructor either implicitly or explicitly using the : base() syntax.
Constructors can take arguments but never have a return type.
They can also be overloaded and overloads can 'chain' to each other using the : this() syntax.
Constructors do not necessarily have to be public but can be internal, protected or private as well. The latter two modifiers prevent an object from being instantiated directly by external code.
If you don't provide a constructor for a class, the system provides a public parameterless one automatically which does nothing except call its base class's parameterless constructor. In the case of an abstract class the default constructor is protected rather than public.
Posted Jun 11, 2014, 10:19 AM EDT