Is it possible to rewrite this C# code into java?
public interface IEnumerable<out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
I have problem creating generic class with parameter T in its name. I also don’t know, how to solve out parameter, which Java don’t support. Anyone who is experienced in both platforms?
The
outhere isn’t for anoutparameter in the normal sense – it’s expressing the fact thatIEnumerable<T>is covariant inT. That allows you to say, for example:Java deals with generic variance very differently, basically. In fact, generics in Java are very different in general – look at the Java Generics FAQ for a lot more detail.
The near-equivalent of
IEnumerable<T>in Java isIterable<T>– you’d be far better off using that than copyingIEnumerable<T>across. (For example,Iterable<T>has language support in Java in the form of the “enhanced for loop”.)