Pregunta

For example, methods that return Task objects are suffixed with "Async". Or methods that use IAsyncResult are prefixed with "Begin" and "End".

Is there any convention for methods that return IEnumerable<T>? It feels like there should be, but I haven't seen any iterator methods in the .NET library.

If the answer is "no", feel free to suggest something as part of your answer.

¿Fue útil?

Solución

I haven't seen any iterator methods in the .NET library

There are plenty, you just haven't recognized them as iterators. Which is pretty good design, it shouldn't be obvious. Lots of them have the obvious name: GetEnumerator(). Appropriate on any class that acts as a collection, it implements IEnumerable<> so you don't even see the name since you use for each to iterate in the client code.

The Linq extension methods are noteworthy, they are all iterators. The naming convention here is to pick their name based on the operation they perform. Like Select() and Where(), surely you know them. That's based on the natural mapping of their operation to a well-known SQL keyword. Doubtful to have something similar in your own program.

Then there's a bunch of them that are named GetXxxs() where Xxxs is the plural of the name of the element they return.

So consider GetEnumerator() first if it is appropriate for the class to implement IEnumerable<>. And pick GetXxxs() next. If you really want to make it obvious that it is an iterator then you could use EnumerateXxx().

Otros consejos

There is no convention. It's not even sure that something returning an IEnumerable is actually iterating using yield return or simply returning an already existing collection that implements IEnumerable. I would not want an extra naming scheme either.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top