When writing a method that takes a string and populates a poco based on it, is it better to have a static Parse(string s) method like Int32.Parse() or overload the constructor so it takes a string?
When writing a method that takes a string and populates a poco based on
Share
I prefer the constructor version, but including both is easy, since the constructor can just call
Parse. This is the pattern followed by theGuidstruct (and likely others as well.)I should add that if you’re not dealing with a
struct, then thestaticmethod should probably be referring to the constructor (or even a separate method that both can call) since you can’t assign tothisin aclassconstructor.EDIT: As TrueWill points out, if you do include
Parse, you should includeTryParseas well. Incidentally,Guidis once again instructive: theParsemethod actually usesTryParse, and just throws an exception ifTryParsereturnsfalse.