The code you suggested is called builder pattern. Here is how I do builder pattern in my C# codes.
Builder class
class PersonInfo
{
private string name, animan, color;
private int age, num;
private PersonInfo() { }
public class Builder
{
PersonInfo info = new PersonInfo();
public Builder setName(string name) { info.name = name; return this; }
public Builder setAge(int age) { info.age = age; return this; }
public Builder setFavoriteAnimal(string animan) { info.animan = animan; return this; }
public Builder setFavoriteColor(string color) { info.color = color; return this; }
public Builder setFavoriteNumber(int num) { info.num = num; return this; }
public PersonInfo build()
{
return info;
}
}
}
and here is how you can use it.
PersonInfo.Builder personInfoBuilder = new PersonInfo.Builder();
PersonInfo result = personInfoBuilder
.setName("MISTAKE")
.setAge(20)
.setFavoriteAnimal("cat")
.setFavoriteColor("black")
.setName("JDM")
.setFavoriteNumber(7)
.build();
0
solved How to call the method successively in C#? [closed]