c# - An example for supporting foreach without implementing IEnumerable -
i looking @ this blog explains foreach
can supported without implementing ienumerable
. not go details of implementation.
i looking example of how support foreach
without implementing ienumerable
.
edit: @sam 's comment, got looking for. (see answer below)
here class doesn't implement ienumerable
, or interfaces @ all:
public class foo { public ienumerator<int> getenumerator() { yield return 1; yield return 2; } }
you can foreach
so:
foreach (int n in new foo()) console.writeline(n);
and print:
1
2
Comments
Post a Comment