I don't think there's much point in returning a new array in your getter when _redirectionRules
is null
. If you set the property in your code that parses the CSV, then it will be cached.
In other words, somewhere you should have a function like this to parse the CSV data (as an example, I've put it in the RedirectionRule
class, but you could have a RedirectionRuleParser
class or something like that depending on your needs):
class RedirectionRule {
public static RedirectionRule Parse(string text) {
// some code here to parse text for your RedirectionRule object
}
public static RedirectionRule[] ParseCsv(string csv) {
string[] values = csv.Split(',');
RedirectionRule[] rules = new RedirectionRule[values.Length];
for (int i = 0; i < values.Length; i++) {
rules[i] = RedirectionRule.Parse(values[i]);
}
}
}
Then, if you have code like this somewhere, you are caching the data:
string csv = "RuleType1,RuleType1,RuleType1";
RedirectionRules = RedirectionRule.ParseCsv(csv);
Elsewhere, where you want to access the data you have cached:
if (RedirectionRules != null) {
// do something with your cached data
} else {
// I don't know, throw an exception or something
}
The only thing your example code would accomplish by creating a new RedirectionRule[MAXLENGTH]
array in your property's getter would be to sneak past the RedirectionRules != null
check above, thereby opening up the possibility of accessing data that looks like it's been cached but really came out of thin air.