I am creating a class (let's call it listClass
) that provides a number of lists that get pulled from the database and cached for future use. One of these is to return a list of Regions for a given Country. I currently have it built as a method:
public static List<string> Regions(string country)
{
List<string> regions;
string cacheKey = "CachedRegions_" + country;
// try to get regions from cache
if (regions == null)
{
// load regions from database
// cache regions
}
return regions;
}
It is currently accessed thusly:
List<string> myRegions = listClass.Regions(myCountry);
However, as what I am accessing is much closer to a property than a method (and every other list in the class is a property), I feel it would be more appropriate to be able to access it like so:
List<string> myRegions = listClass.Regions[myCountry];
Note that I want to be able to cache the Regions for each Country separately (so I'm not caching/loading every region in the world each time I touch the property). Is there a way to create Regions
as a property (that can be accessed via a key) rather than as a method?