This C# doesn't compile:
public class IdList<T> where T : IdList<T>.Item {
List<T> List = new List<T>();
public T this[int id] {
get => List[id];
set { }
}
public class Item {
public int id;
// Not shown: id used for equality and hash.
}
}
The complaint from the compiler is:
The type 'IdList' already contains a definition for 'Item'
If I comment out the indexer, it compiles.
How can I get this to compile? Rider doesn't have a fixit.
The unstylish workaround is not to nest the Item class.
IDE is Rider 2018.1.4, Language level 7.2, on macOS.
So you should not declare twice the generic : in the class and its inner class. The E type is indeed visible in the inner class as inner classes have access to other members of the enclosing class (including the generic of the class).
The generic class works with multiple data types. A normal class works with only one kind of data type.
The issue is that the indexer, when compiled to .NET byte code, becomes a property called "Item". You need to change your type name to something else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With