Indexer cannot be static
CodeRush Classic shows the Indexer cannot be static code issue if an indexer is marked static.
#Fix
Remove the static modifier from the indexer declaration.
#Purpose
Highlights the declaration statements, which would cause the The modifier ‘static’ is not valid for this item compilation error.
#Example
public class MyClass
{
public static string │this[int i]
{
get
{
return _Strings[i];
}
set
{
_Strings[i] = value;
}
}
private static List<string> _Strings;
public static void AddString(string value)
{
if (_Strings == null)
_Strings = new List<string>();
_Strings.Add(value);
}
}
Fix:
public class MyClass
{
public string this[int i]
{
get
{
return _Strings[i];
}
set
{
_Strings[i] = value;
}
}
private static List<string> _Strings;
public static void AddString(string value)
{
if (_Strings == null)
_Strings = new List<string>();
_Strings.Add(value);
}
}