Ho un HashSet e sto cercando di gettare in IReadOnlyCollection, ma sto ottenendo errore:Impossibile convertire HashSet a IReadOnlyCollection
Cannot implicitly convert type 'System.Collections.Generic.HashSet' to 'System.Collections.Generic.IReadOnlyCollection'. An explicit conversion exists (are you missing a cast?)
HashSet è un
public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback, ISet<T>, IReadOnlyCollection<T>
posso usare cast esplicito , ma non conosco il motivo per cui non posso semplicemente usarlo come IReadOnlyCollection.
HashSet<DateTime> set = new HashSet<DateTime> { DateTime.Today };
ICollection<DateTime> collection = set; // OK
ISerializable serializable = set; // OK
IDeserializationCallback deserializationCallback = set; // OK
ISet<DateTime> iSet = set; // OK
IReadOnlyCollection<DateTime> castReadOnlyCollection = (IReadOnlyCollection<DateTime>)set; // OK
IReadOnlyCollection<DateTime> readOnlyCollection = set; // Error
Perché non posso usarlo senza un cast esplicito?
Sto usando .NET Framework 4.5
E a quanto pare non ha sempre implementare tale interfaccia http: // StackOverflow .com/questions/11849861/why-hashsett-does-not-implement-ireadonlycollectiont – juharr
È stato aggiunto oops in .NET 4.5, indotto da IReadOnlyCollection <> per supportare la proiezione del linguaggio WinRT e WinRT non ha un equivalente di un set. È stato risolto in 4.5.1, è sufficiente modificare l'obiettivo quadro del progetto come soluzione alternativa. –