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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Jun 11, 2014, 2:19 PM
VulpesPosted Jun 11, 2014, 1:11 PM
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.