How to call the method successively in C#? -
i saw code typed in java.
personinfo result = personinfobuilder .setname("mistake") .setage(20) .setfavoriteanimal("cat") .setfavoritecolor("black") .setname("jdm") .setfavoritenumber(7) .build();
and tried in c# way.
public class personinfo { public int age { get; set; } public string name { get; set; } } public class personbuilder { private personinfo _personelement = null; private int age; private string name; public void setage(int age) { this.age = age; } public void setname(string name) { this.name = name; } public personinfo getperson() { _personelement = _personelement ?? new personinfo(); _personelement.age = age; _personelement.name = name; return _personelement; } } class program { static void main(string[] args) { personbuilder builder = new personbuilder(); builder.setname("he") .setage(20); } }
and have error [resource not available anonymous access. client authentication required.],
how can call method successively in c# java ?
the code suggested called builder pattern. here how builder pattern in 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 how 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();
Comments
Post a Comment