What are the differences between Constructors and Methods?
Loading
What are the differences between Constructors and Methods?
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.
Aravind GovindarajPosted Oct 19, 2022, 3:00 PM
A contractor is one who is called automatically while you instantiate the class, however methods we need to call manually. In a real-time scenario, In a new model car/motorbike, while you start an engine, dim lights are auto-on without any manual operation( constructor case). however, if you want to press the horn then we have to do it manually that is called as a method. hope this helps you better
Toufik El AzzaouiPosted Oct 19, 2022, 2:57 PM
Constructors are used to initialize the state of the object. Like methods, a constructor also contains a collection of statements that are executed when the object is created. Whenever an object is created using the new() keyword, at least one constructor (it may be the default constructor) is called to assign initial values to the data members of the same class.
Brahma Prakash ShuklaPosted Oct 19, 2022, 11:45 AM
Uday DodiyaPosted Sep 13, 2022, 3:48 AM
Constructors are special methods used to initialize objects whereas methods are used to execute certain statements. Following are the important differences between Constructors and Methods.
Example of Constructor vs Method
Output :
Constructor invoked. num: 15
Method invoked. num: 10
Vishal YelvePosted Sep 12, 2022, 3:04 PM
A constructor is a special method that usually has the same name as the class, and we can use it to set the values of the members of an object to either default or user-defined values. Whereas, a method is a programmed procedure that is defined as part of a class and included in any object of that class. These definitions give an idea about the fundamental difference between constructor and method.
Return type : To add to this, the constructor has no return type whereas method can return a value or not. Hence, this is another difference between constructor and method.
Default : An important difference between constructor and method is that the program will call the default constructor in case the programmer does not write a constructor. However, there are no default methods.
Name: A constructor has the same name as the class name while a method can have any name other than keywords.
Invocation : One other difference between constructor and method is that the constructors implictly invoke whereas the methods invoke explicitly.
Usage : Furthermore, constructor helps to initialize an object whereas a method helps to exhibit the functionality of an object.
Mahesh ChandPosted Sep 12, 2022, 1:58 PM
A constructor is a method.
A constructor is called and code is executed when a new instance of a type is created. So, you want to put code in a constructor that you want automatically be executed when a new instance is created such as initialization of variables.
Rajanikant HawaldarPosted Sep 12, 2022, 9:30 AM
Constructors
Purpose of cunstructor is to create an instance of a class
Modifiers cannot be abstract, sealed, native, static, or synchronized
No return type, not even void
Same name as the class (first letter is capitalized by convention) -- usually a noun
'this' Refers to another constructor in the same class. If used, it must be the first line of the constructor
'base' calls the constructor of the parent class. If used, must be the first line of the constructor
Constructors are not inherited
Methods
Purpose is to group of C# statements
Modifiers can be abstract, sealed, native, static, or synchronized
Return type is void or a valid return type
Any name except the class.
'this' refers to an instance of the owning class. Cannot be used by static methods.
'base' calls an overridden method in the parent class
Methods are inherited
Amit MohantyPosted Sep 12, 2022, 5:43 AM
The important difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.
Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new.
The definitions of constructors and methods look similar in code. They can take parameters, they can have modifiers (e.g. public ), and they have method bodies in braces.
Constructors must be named with the same name as the class name. They can't return anything, even void (the object itself is the implicit return).
Methods must be declared to return something, although it can be void.
Jignesh KumarPosted Sep 12, 2022, 5:06 AM
Hi Anand,
There are few major difference among Constructor and Method, Please find as below,
Sachin SinghPosted Sep 11, 2022, 6:10 PM
Constructor initializes the uninialized member variables of a class and are necessary for object creation. When we create an object with new keyword the parameter-less constructor is called that initializes all variables of the class which are not initialized before, to their default values like int to zero and string to null.
Methods are like small piece of code which does some action and may or may not retrurn something. for example a piece of code can save data to DB and can return a bool flag indicating sucess or can also be void.