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

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -