Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

methods and constructors

Tags:

c#

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

like image 886
tintincutes Avatar asked Jul 15 '09 08:07

tintincutes


People also ask

Can you use methods in constructor?

A constructor can call methods, yes. A method can only call a constructor in the same way anything else can: by creating a new instance. Be aware that if a method constructs a new object of the same type, then calling that method from a constructor may result in an infinite loop...

What makes a method a constructor?

A constructor method is a special function that creates an instance of the class. Typically, constructor methods accept input arguments to assign the data stored in properties and return an initialized object.

Are methods and constructors same in Java?

In Java, constructors must be called with the same name as the name of the class in which they live, whereas methods can have any name and can be called directly either with a reference to the class or an object reference. Constructors are an exception to this rule.

What is a constructor method example?

A constructor in Java is similar to a method that is invoked when an object of the class is created. Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example, class Test { Test() { // constructor body } } Here, Test() is a constructor.


7 Answers

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.

public class MyType
{
    private SomeType _myNeeds;

    // constructor
    MyType(SomeType iWillNeedThis)
    {
        _myNeeds = iWillNeedThis;
    }

    // method
    public void MyMethod()
    {
        DoSomethingAbout(_myNeeds);
    }
}
like image 194
Serhat Ozgel Avatar answered Oct 07 '22 15:10

Serhat Ozgel


A constructor is a method.. a special method that is being called upon "construction" of the class.

Definition: A constructor is a class member function in C++ and C# that has the same name as the class itself.

The purpose of the constructor is to initialize all member variables when an object of this class is created. Any resources acquired such as memory or open files are typically released in the class destructor.

From About.com

like image 39
Chris Avatar answered Oct 07 '22 13:10

Chris


Constructor will be automatically invoked when an object is created whereas method has to be called explicitly. Constructor needs to have the same name as that of the class whereas functions need not be the same. There is no return type given in a constructor signature (header). It is same as that of the Class There is no return statement in the body of the constructor.

Example:

class Widget //Some Class "Widget"
{
    int _size;
    int _length;
// Declaring a Constructor, observe the Return type is not required

public Widget(int length)
    {
        this._length = length;
    }
// Declaring a Method, Return type is Mandator

    public void SomeMethod(int size)
    {
        this._size = size;
    }
}

//Calling the Constructor and Method

class Program
{
    static void Main()
    {

//Calling the Constructor, Observe that it can be called at the time the Object is created
        Widget newObject = new Widget(124);

//Calling the Method, Observe that the Method needs to be called from the New  Object which has been created. You can not call it the way Constructor is called.

        newObject.SomeMethod(10);I

    }
}
like image 26
user2103288 Avatar answered Oct 07 '22 13:10

user2103288


A constructor is an instance method with special meaning - specifically it is called internally when creating an instance of the corresponding class with new. That's the key difference.

Other minor differences are that the constructor must have the same name as the class it belongs to and it can't have any return value, even void.

like image 43
sharptooth Avatar answered Oct 07 '22 15:10

sharptooth


The constructor method name has the same name as the class. Also, it does not have a return type. Or, if you will, the constructor method itself has no name, but the return type is the class type.

public class Foo
{
    // Constructor
    public Foo()
    { }

    public void Bar()
    { }
}
like image 42
Ionuț G. Stan Avatar answered Oct 07 '22 13:10

Ionuț G. Stan


Note that the material of constructor is method. it means that constructor is a special type of method, its name is the same as class exactly but does not have output paramether.Constructor is called during creation of the object from class.

Public class person()
  {
     public person()
     {
     }
   }
like image 25
Arash Afshinfar Avatar answered Oct 07 '22 15:10

Arash Afshinfar


In terms of what they're allowed to do, constructors don't differ all that much from methods. The main conceptual difference between a method and a constructor is its purpose.

A constructor brings an object into a valid, usable state, and is called only once, at the beginning. A method changes an object from one valid state to another. (Okay, some methods only retrieve information, they're not required to change the state of an object).

Edit: It occurs to me that, particularly for C#, the above explanation might be confusing, as immutable objects aren't exactly uncommon idioms in the language, so a lot of objects the OP will encounter won't have a changeable state. Complex concepts often have one-line explanations that are simple, elegant and wrong.

like image 32
Michiel Buddingh Avatar answered Oct 07 '22 15:10

Michiel Buddingh