Get benefited from interview Q/A and Online Test section. Subscribe to this blog to get updates directly in your mail box.
Best View in Google Crome, Mozilla firefox or IE 7 or above

Sunday, April 6, 2014

Difference between constructor and method in c#

This is a very basic question mainly asked in Beginner level interviews (1 to 2 years of experience).
The interviewer can twist the question as below as well.


  • Difference between method and constructor in c#
  • Difference between constructor and method in dotnet with example
  • Difference between methods and constructors in object oriented programming


Answer:

Here are few differences between Constructor and methods in bullet points.


Constructor:
1. Name of constructor should be same as class name
2. Constructor is used to initialize class members (variables). It is used to create an instance of  a class.
3. Returns nothing. Hence no return type.
4. Called implicitly at the time of object creation


Method:
1. Name is independent from class name
2. Method is having some functionality. It is used to perform some operations.
3. Must return something. If nothing to return, explicitly return type should be void.
4. Need to be called explicitly


No comments:

Post a Comment

  © Blogger templates Shiny by Ourblogtemplates.com 2008

Back to TOP