I have a HashSet and I am trying to cast it into IReadOnlyCollection, but I am getting error:
Cannot implicitly convert type 'System.Collections.Generic.HashSet' to 'System.Collections.Generic.IReadOnlyCollection'. An explicit conversion exists (are you missing a cast?)
Hashset is a
public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback, ISet<T>, IReadOnlyCollection<T>
I can use explicit cast, but I don't know the reason why I can't just use it as 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
Why can't I use it without an explicit cast?
I am using .NET framework 4.5