Update: I appreciate all of the comments, which have essentially comprised unanimous opposition. While every objection raised was valid, I feel that the ultimate nail in the coffin was Ani's astute observation that, ultimately, even the one miniscule benefit that this idea ostensibly offered -- the elimination of boilerplate code -- was negated by the fact that the idea itself would require its own boilerplate code.
So yeah, consider me convinced: it would be a bad idea.
And just to sort of salvage my dignity somewhat: I might have played it up for argument's sake, but I was never really sold on this idea to begin with -- merely curious to hear what others had to say about it. Honest.
Before you dismiss this question as absurd, I ask you to consider the following:
IEnumerable<T>
inherits from*IEnumerable
, which means that any type that implementsIEnumerable<T>
generally must implement bothIEnumerable<T>.GetEnumerator
and (explicitly)IEnumerable.GetEnumerator
. This basically amounts to boilerplate code.- You can
foreach
over any type that has aGetEnumerator
method, as long as that method returns an object of some type with aMoveNext
method and aCurrent
property. So if your type defines one method with the signaturepublic IEnumerator<T> GetEnumerator()
, it's legal to enumerate over it usingforeach
. - Clearly, there is a lot of code out there that requires the
IEnumerable<T>
interface -- for instance, basically all of the LINQ extension methods. Luckily, to go from a type that you canforeach
on to anIEnumerable<T>
is trivial using the automatic iterator generation that C# supplies via theyield
keyword.
So, putting this all together, I had this crazy idea: what if I just define my own interface that looks like this:
public interface IForEachable<T>
{
IEnumerator<T> GetEnumerator();
}
Then whenever I define a type that I want to be enumerable, I implement this interface instead of IEnumerable<T>
, eliminating the need to implement two GetEnumerator
methods (one explicit). For example:
class NaturalNumbers : IForEachable<int>
{
public IEnumerator<int> GetEnumerator()
{
int i = 1;
while (i < int.MaxValue)
{
yield return (i++);
}
}
// Notice how I don't have to define a method like
// IEnumerator IEnumerable.GetEnumerator().
}
Finally, in order to make this type compatible with code that does expect the IEnumerable<T>
interface, I can just define an extension method to go from any IForEachable<T>
to an IEnumerable<T>
like so:
public static class ForEachableExtensions
{
public static IEnumerable<T> AsEnumerable<T>(this IForEachable<T> source)
{
foreach (T item in source)
{
yield return item;
}
}
}
It seems to me that doing this enables me to design types that are usable in every way as implementations of IEnumerable<T>
, but without that pesky explicit IEnumerable.GetEnumerator
implementation in each one.
For example:
var numbers = new NaturalNumbers();
// I can foreach myself...
foreach (int x in numbers)
{
if (x > 100)
break;
if (x % 2 != 0)
continue;
Console.WriteLine(x);
}
// Or I can treat this object as an IEnumerable<T> implementation
// if I want to...
var evenNumbers = from x in numbers.AsEnumerable()
where x % 2 == 0
select x;
foreach (int x in evenNumbers.TakeWhile(i => i <= 100))
{
Console.WriteLine(x);
}
What do you guys think of this idea? Am I missing some reason why this would be a mistake?
I realize it probably seems like an overly complex solution to what isn't that big of a deal to start with (I doubt anybody really cares that much about having to explicitly define the IEnumerable
interface); but it just popped into my head and I'm not seeing any obvious problems that this approach would pose.
In general, if I can write a moderate amount of code once to save myself the trouble of having to write a small amount of code lots of times, to me, it's worth it.
*Is that the right terminology to use? I'm always hesitant to say one interface "inherits from" another, as that doesn't seem to properly capture the relationship between them. But maybe it's right on.