Constructor Chaining vs Similar ones

I have the following constructors:

public Class1()
{
    this.Variable = new Variable();
}

public Class1(Variable vari)
{
    this.Variable = vari;
}

Which can be rewritten as:

public Class1()
    : this(new Variable())
{

}

public Class1(Variable vari)
{
    this.Variable = vari;
}

Is there any benefit from writing it the second way or would they be practically the same?

The first one duplicates logic. Even if said logic consists of a single assignment statement.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *