One liners

October 12, 2007

OK, can’t really take credit for this one (thx Cristian), but wanted to share a C# one liner with you. If you know me and seen my code you should know that I love those one line statements, especially for properties.

So here it is, a one line lazy load property:
private ArrayList _lazyLoadedList;
public ArrayList LazyLoadedList
get { return _lazyLoadedList ?? (_lazyLoadedList = new ArrayList()); }

And while we’re sharing, you can know that this is how my standard ViewState properties has been looking since 2.0.
public virtual string Text
get { return (string)(ViewState["Text"] ?? string.Empty); }
set { ViewState["Text"] = value; }

Damn I love that ??-operator!