C#: Cached Property: Easier way?

Posted by Peterdk on Stack Overflow See other posts from Stack Overflow or by Peterdk
Published on 2010-04-05T15:50:07Z Indexed on 2010/04/05 15:53 UTC
Read the original article Hit count: 591

Filed under:
|
|

I have a object with properties that are expensive to compute, so they are only calculated on first access and then cached.

 private List<Note> notes;
 public List<Note> Notes
    {
        get
        {
            if (this.notes == null)
            {
                this.notes = CalcNotes();
            }
            return this.notes;
        }
    }

I wonder, is there a better way to do this? Is it somehow possible to create a Cached Property or something like that in C#?

© Stack Overflow or respective owner

Related posts about c#

Related posts about property