In my OverloadedIndexer class I have a private array field called stringData, which size is set in the class constructor. I want to create an indexer with an optional parameter. It's default value should be the number of items in that array.
Unfortunately, the code line at the bottom of this post raises the following compile-time error:
An object reference is required for the non-static field, method, or property 'OverloadedIndexer.stringData'
public string this[string data, int maxOccurences = stringData.Length]
Is there a way to make it work?
Default values have to be constants - you can't provide one which depends on another variable. So this would be fine:
but what you've got isn't valid. Admittedly the error message you're getting is for a slightly different reason, but even if you could get past that, you'd run into the constness issue.