In C #, if you have multiple constructors, you can do something like this:
public MyClass(Guid inputId, string inputName){
The idea, of course, is to reuse the code. However, what is the best approach when a little complex logic is required? Let's say I need this constructor:
public MyClass(MyOtherClass inputObject) { Guid inputId = inputObject.ID; MyThirdClass mc = inputObject.CreateHelper(); string inputText = mc.Text; mc.Dispose();
The caveat here is that I need to create an object that has , after use. (Clarification: not right away, but I have to call Dispose () and not wait for garbage collection)
However, I did not see a way to call the base constructor again if I add code inside my overloaded constructor. Is there a way to call the base constructor from overloaded?
Or you can use
public MyClass(MyOtherClass inputObject): this(inputObject.ID, inputObject.CreateHelper().Text) {}
Will it automatically remove the generated object from CreateHelper ()?
Edit: Thank you. Two problems: I do not control MyOtherClass, and I have no extension methods (only .NET 3.0 ...). However, I manage my own class, and since I just started writing it, I have no problem reorganizing the constructors if there is a good approach.
Michael stum
source share