dotnet / dotnet/dotnet-api-docs

Please add a remark to emphasise the potential weirdness with TKey being an enum and index 0

Aperta
#963 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area-System.Collections dotnet-api/prod Pri2 untriaged
Lingua principale
C#
Stelle
949
Fork
1.7k
Merge medio
2g 19h
PR unite (30g)
52

Descrizione

Hi!
I just ran into a weird bug using a `KeyedCollection`.
Consider the following code
```
public class TestClass {
public TestKey TestKey;
}

public enum TestKey {
Entry1,
Entry2
}

public class TestKeyedCollection : KeyedCollection {
protected override TestKey GetKeyForItem(TestClass item) {
return item.TestKey;
}
}

public class Test {
public void RunTest() {
var collection = new TestKeyedCollection();
collection.Add(new TestClass() { TestKey = TestKey.Entry2 }); // Note that I add Entry2 first
collection.Add(new TestClass() { TestKey = TestKey.Entry1 });

var a = collection[0]; // would expect element at index 0, so Entry2
var b = collection[1]; // would expect element at index 1, so Entry1
}
}
```
But because of Ecma-334 5th Edition / December 2017 point "11.2.4 Implicit enumeration conversions" (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf) `var a = collection[0]` in my setup implicitly converts to `TestKey.Entry1`, so for both `a` and `b` I get the same `TestClass` with `TestKey` being `Entry1`.

The result might be different on different runtimes, I only tried with Rider 2018.2.3. as IDE for a Unity 2018.1.6f1 project (and therefore some custom mono runtime), but nevertheless it would be very nice if you could add a comment to this documentation stating the potential issue when using an enum as TKey and trying to access the element at index 0.

Thank you very much!
Tim

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 23e7fbba-ff7a-0770-a2c5-9fb11a830556
* Version Independent ID: f374f735-e1ad-b485-cfa9-e34c0c2b5655
* Content: [KeyedCollection<TKey,TItem> Class (System.Collections.ObjectModel)](https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.keyedcollection-2?view=netframework-4.7.2)
* Content Source: [xml/System.Collections.ObjectModel/KeyedCollection`2.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Collections.ObjectModel/KeyedCollection`2.xml)
* Product: **dotnet-api**
* GitHub Login: @mairaw
* Microsoft Alias: **mairaw**

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Apri xml/System.Collections.ObjectModel/KeyedCollection`2.xml e confronta la documentazione dell'API di KeyedCollection con la pagina collegata. Trova la documentazione dell'indicizzatore e aggiungi una nota che descriva la potenziale ambiguità quando TKey è un enum e viene usato l'indice 0. Il lavoro è completato quando l'avviso appare chiaramente nel riferimento API generato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.