user3095439
user3095439

Reputation: 65

About Constructor and methods in c#

What is the reason for initialization using constructor instead of method.

Code 1:

class A {
    int x, y;

    public A() {
        x = 10;
        y = 4;
    }
}

Code 2:

class A {
    int x, y;

    public fun() {
        x = 10;
        y = 4;
    }
}

From above, what is the difference between Code 1 and Code 2. If any one know the answer please clear my doubt.?

Upvotes: 1

Views: 110

Answers (8)

Paulo Lima
Paulo Lima

Reputation: 1238

the constructor is called to create the instance of your class and does not rely on calls, unlike the method

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

Technically, you can hide constructor and use factory method pattern:

class A {
  int x, y;

  // Hidden constructor 
  protected A() {
    x = 10;
    y = 4;
  }

  // Factory method to create A class instances
  public static A Create() {
    ...
    A result = new A();
    ...
    return result;  
  }
}

In your example factory method is an overshoot, but it could be helpful when implementing singleton, strategy patterns etc.

Upvotes: 0

Tapesh
Tapesh

Reputation: 372

Constructor used as initializer , because of it provide the functionality to initializing the object to memory or when a object is created at that time constructor will call first. If you don't initialize any value separately and you want to use some variable in your result then you can use constructor as initializer. Why constructor use in coding? Bcz it have the nature to initialize some thing before of creation of Object.

I think you got my point. happy learning. :)

Thanks

Upvotes: 0

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

It is guaranteed that constructor will get called when object is created but in case of method it is your control when to call. If you initialize values in method instead of constructor, there may be side effect if you call the method at wrong time.

Therefore it is always good practice to initialize the values in constructor instead of methods and that is the purpose of constructor.

Upvotes: 3

TheLostMind
TheLostMind

Reputation: 36304

A constructor is implicitly called when an object is created. In second case, the method is an instance level method.. You will have to create an object of type A and then "change" the values of x and y... The defualt constructor will assign default value (0) to your int variables...

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236278

First assignment occurs when instance is created. Second assignment occurs when you will execute the fun() method.

BTW you can call fun() method in constructor.

Upvotes: 2

EdgeCase
EdgeCase

Reputation: 4827

A constructor ensures that the object is properly instantiated. There's not guarantee that the caller will call the method to manually instantiate the object.

Upvotes: 1

mnemonic
mnemonic

Reputation: 1645

When using code 1, A a = new A(); will init x and y.

When using code 2, you need A a = new A(); and a.fun(); to init x and y.

Init of your variables should be placed within a method if you wanna use this method also for additional inits sometime later.

btw fun is not an adequate name for a method.

Upvotes: 0

Related Questions