In a class that has a lazy loaded property, for example:
private Collection<int> someInts; public Collection<int> SomeInts { get { if (this.someInts == null) this.someInts = new Collection<int>(); return this.someInts; } }
Is it worth it to also have such a property as:
public bool SomeIntsExist { get { return (this.someInts != null && this.someInts.Count > 0); } }
And then using this property .. for example:
if (thatClass.SomeIntsExist) { // do something with thatClass.SomeInts collection }
or is it premature optimization. It is certainly easier to roll with something like below, but it will instantiate the collection to no avail:
if (thatClass.SomeInts.Count > 0) { // do something with thatClass.SomeInts collection }
Is the compiler smart enough to understand such things? Is there a better way?
Paulg source share