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 out
here isn't for an out
parameter in the normal sense - it's expressing the fact that IEnumerable<T>
is covariant in T
. That allows you to say, for example:
IEnumerable<string> strings = new List<string>();
IEnumerable<object> objects = strings;
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 is Iterable<T>
- you'd be far better off using that than copying IEnumerable<T>
across. (For example, Iterable<T>
has language support in Java in the form of the "enhanced for loop".)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With