c# - Nullable Bool in Getter Method -
the team discussing @ length how handle rather large account
object.
we don't want have initialize every property in constructor, overhead property might never used on web page. came following.
public partial class account { private bool? projecho; public bool projectecho { { if (!projecho.hasvalue) { projecho = isprojectecho(); return (bool)projecho; } else { return (bool)projecho; } } } }
this way, if using account object, , needs access property setter happens internally. aren't big fan of casting technique, way ensure have true/false value when code has ran. looks there wrong approach, seems efficient way have property filled in when need it.
my question is: "is there better alternative achieve trying accomplish development standard perspective?"
there's nothing wrong concept - lazy initialization.
you keep backing field property null, , assign value when first used. don't need cast nullable bool bool, can use nullable.value instead.
private bool? projecho; public bool projectecho { { if (!projecho.hasvalue) projecho = isprojectecho(); return projecho.value; } }
seems nicer me. there support in .net lazy initialization:
https://msdn.microsoft.com/en-us/library/dd997286(v=vs.110).aspx
but haven't used personally.
Comments
Post a Comment