KentZhou KentZhou - 7 days ago 7
C# Question

How to call constructor inside the class?

I want to call constructor inside the class like:
public class Myclass(){

public MyClass(){
//......
}

public MyClass(int id):this(){
//......
}

private void Reset(){
//.....
this = new MyClass(id); //here I want to call constructor
//......
}
}


but it is not working. Is it possible and how can I do it if Yes?

Answer

You can't. But what you could do is split the constructor logic into an Initialize method that then reset could call.

   public MyClass(){
   //......
   }

   public MyClass(int id):this(){
      Initialize(id);
   }

   private void Initialize(int id){
   //....
   }

   private void Reset(){
      //.....
      Initialize(id);
      //......
   }
}