Did everyone else other than me know that in .NET, any custom type has a default constructor ? I was under the impression till now that only value types have a default constructor, a parameterless constructor supported by default if there is no other constructor declared in the type. But while doing a code review yesterday, i saw that the developer had written a Factory that creates types based on the runtime request. The types that he was creating implemented a single Interface. Till here it is crystal ! But then i stepped into the type definition and found that there was no constructor defined for the type. The app works perfectly to my surprise !
Learned something useful but at this point i was stuck with a very weird question !
Why do we need constructors ? What is its sole purpose ?
Now if you are going to point out that they are used for allocation of memory, then you are wrong. That's what someone whom i work with did but the whole point of the constructor is that it gets called after the allocation of memory has happened. If that is not so, then how will you initialize the member data in the constructor ?
Basically when i say
Object myObj = new Object() ;
I mean that the variable myObj needs to be allocated the memory actually required ( as specified in the metadata for Type Object ) so that further usage of the variable from then on is possible. But why call again a constructor there if new is the keyword handling the allocation of memory part ? The way i see it, new is like one of those operators ( +, -= etc. ) which takes in as argument the object which needs to be allocated some memory and the Type to which the object belongs to. hmm does anybody else see my point here ? Why the constructor ?
The way i see it ( with my limited knowledge ofcourse ! ) is that the constructor is part of the event mechanism, wherein the allocation of the memory for the particular Type based on its metadata by the runtime is the event, and on completion of which the object of the specific Type is subscribed by default to the kind-of MemoryAllocationComplete handler and eventually the constructor gets invoked after memory allocation ! Does that make sense ?
I've been asking this particular question for quite sometime to myself. Why do all memory allocations to objects have to involve the usage of a constructor ? Was such a concept used because there needs to be a definite entry point and a destructor was used because there needs to a definite end point of the object usage ? What is it ?
Or is it because a constructor is required to initialize the pointer to the Virtual Method Table which is used to find the virtual methods for the Type. If there is no pointer, the system might jump off to some unknown location and might try to execute whatever code happens to be there in that location and probably could do almost anything at that unknown and undefined point in the code. But couldn't the initialization of the pointer be done somewhere else ?
On a sidenote, i might have answered my own question here by saying constructors are used for initialization. But aren't there other ways to do the same ? Aren't there other patterns that can be used to initialize data members of a particular Type without the usage of constructors ?
I know here that i've questioned the basics of OOP but IMHO many people including me, do not really understand the specifics behind the pattern. There is probably nothing that i hate about constructors as such but this particular question as to why we've been using a construct such as this, a construct which nothing but shrinks two statements from a layman's perspective.
Object myObj as new ;
myObj.Init();
Into
Object myObj = new Object() ; // Which might in turn call Init for all we know ;)
Probably, i'd be at peace and might get more work done if i had just accept blindly the fact that Constructor pattern is the only best pattern suitable for the situation but aha that's not been the case. If anyone has had some enlightening insights about this, then feel free to post them :)