Skip to main content

Posts

Showing posts with the label Yield

C# 2.0 - Iterators

Hoje vim para falar de uma das features mais úteis do C# 2.0, mesmo que muitas pessoas não saibam do que se trata. Vamos falar de iterators ! O que é um iterator? Para quem ainda não sabe o que é o padrão  iterator , podem ler a sua descrição  aqui . Um iterator  é uma classe/objecto que sabe como percorrer uma estrutura. Portanto, se tivermos uma lista de objectos, um iterator  teria o conhecimento de como percorrer essa lista e aceder cada elemento da lista. O iterator  é um design pattern  bastante conhecido e está por trás de muitas coisas fantásticas que temos hoje no .NET. (assim de repente lembro-me de LINQ ). A Microsoft definiu duas interfaces para este design pattern  chamadas IEnumerable e IEnumerator . A primeira é implementada no objecto que vamos percorrer, a segunda é no objecto que irá percorrer o primeiro. Porque é que é uma feature? Diga-se de passagem, os iterators  são um conceito bem conhecido mesmo antes do .NET existir...

The evolution of C# - Part III - C# 2.0 - Iterators

It's been a while since i wrote the last post, but i did not forget my purpose of creating a series that shows the evolution of C#. Today i came here to talk about one of the most useful features of C#, even if you dont know you're using it. Let's talk about iterators ! What is an iterator? For those of you who didn't read about the iterator pattern somewhere in the internet or in the "Gang of Four" book, you can read a description  here . The iterator is a class/object/whatever which knows how to traverse a structure. So, if you have a list or collection of objects, an iterator would have the knowledge of how to traverse that collection and access each element that it contains. The iterator is a well known design pattern and is behind many of the wonderful that we have nowadays in .NET (Linq comes to mind). Why is it a feature? Truth be told, an iterator is a concept well known way before .NET even existed. Being an OO Design Pattern, the iterator has...