Description
The SmartEnum.List
currently is implemented as follows.
public static IReadOnlyCollection<TEnum> List =>
_fromName.Value.Values
.ToList()
.AsReadOnly();
This API might be frequently called by the consumers, and creating a new list doesn't make much sense. Moreover, this is a property, and the consumer expects that the properties return precalculated fields. They shouldn't contain heavy operations or create additional allocations. If that's the case, then usually the API should be defined as a method, e.g. GetList()
.
We already keep the collection of discovered enums as Lazy<TEnum[]>
. But, even internally this collection is never altered, so I suggest the state be kept as ReadOnlyCollection
instead of an array, and List
simply would return this field.
static readonly Lazy<ReadOnlyCollection<TEnum>> _enumOptions =
new Lazy<ReadOnlyCollection<TEnum>>(GetAllOptions, LazyThreadSafetyMode.ExecutionAndPublication);
public static IReadOnlyList<TEnum> List => _enumOptions.Value;
Note: The ReadOnlyCollection
is proposed since not all targetted TFMs support newer collection types.