Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 12, 2022, 5:30 AM EDT
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
Mahesh ChandRank #1 · 393.3K reputationPosted Sep 12, 2022, 9:58 AM EDT
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.
Sachin SinghRank #66 · 26.2K reputationPosted Sep 11, 2022, 2:10 PM EDT
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.
public class ClassA { public int _x; public ClassA() { } public ClassA(int x) { this._x=x; } } Public Class ClassB { public void Method1() // this is method { ClassA obj = new ClassA(); // default constructor of ClassA is called; ClassA obj = new ClassA(10);// parameter constructor of ClassA is called; } }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.
public bool Save() { // save to db return true; }Amit MohantyRank #112 · 20K reputationPosted Sep 12, 2022, 1:43 AM EDT
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.
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 12, 2022, 11:48 PM EDT
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
public class MyClass { int num; MyClass () { this.num = 15; System.out.println(("Constructor invoked. num: " + this.num)); } public void init() { this.num = 10; System.out.println(("Method invoked. num: " + this.num)); } public static void main(String[] args) { MyClass test = new MyClass (); test.init(); } }Output :
Constructor invoked. num: 15
Method invoked. num: 10
Toufik El AzzaouiRank #3,804 · 389 reputationPosted Oct 19, 2022, 10:57 AM EDT
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.
Aravind GovindarajRank #430 · 5.8K reputationPosted Oct 19, 2022, 11:00 AM EDT
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
Jignesh KumarRank #91 · 22.9K reputationPosted Sep 12, 2022, 1:06 AM EDT
Hi Anand,
There are few major difference among Constructor and Method, Please find as below,
Vishal YelveRank #178 · 13.6K reputationPosted Sep 12, 2022, 11:04 AM EDT
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.
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 19, 2022, 7:45 AM EDT