I have a HashSet and I am trying to cast it into IReadOnlyCollection, but I am getting error:
Cannot implicitly convert type \'System.Collections.Gene
This is because in .NET 4.5, HashSet doesn't implement IReadOnlyCollection. In .NET 4.6, it does, so the implicit cast works as expected.
BTW, the explicit cast in your code snippet compiles, but it will fail at runtime. It compiles because the set variable is of type HashSet, and since HashSet is not sealed, there might be subclasses that implement the interface.