I’m learning C# now and a beginner in the programming world.
I have a book called The Complete Reference by Herbert Schildt. So far its a good book and I’m in the middle of learning about methods and constructors.
I’m quite confused what’s the difference of methods and constructors. Because in the book it has almost the same example. I don’t know how to differentiate them.
I would appreciate your idea.
By the way I have several definition here of both, I just would like to know how to differentiate them
Thanks for your help
Cheers
A constructor only works when you create a new instance of a class. This is the very first method to run on an instance, it has to run, and it runs exactly once.
A method on an instance can be called anywhere between zero times to infinite times on an instance once it is created.
A constructor is run implicitly. When a new instance of a class is created, it runs automatically. A method is run explicitly. It has to be called either from some outside source or from a method -or a constructor- in the class.
A constructor is intended to be used for wiring. In the constructor, you want to avoid doing actual work. You basically prepare the class to be used. A method is intended to do actual work.