Question: After almost 4 years of experience, I haven't seen a code where **yield** keyword is used. Can somebody show me a practical usage (along explanation) of this keyword, and if so, aren't there other ways easier to fullfill what it can do?

Answer: 
Some time ago I had a practical example, let's assume you have a situation like this:

```
List<Button> buttons = new List<Button>();
void AddButtons()
{
   for ( int i = 0; i <= 10; i++ ) {
      var button = new Button();
      buttons.Add(button);
      button.Click += (sender, e) => 
          MessageBox.Show(String.Format("You clicked button number {0}", ???));
   }
}

```

The button object does not know his own position in the collection. The same limitation applies for `Dictionary<T>` or other collections types.

Here is my solution using `yield` keyword:

```
interface IHasId { int Id { get; set; } }

class IndexerList<T>: List<T>, IEnumerable<T> where T: IHasId
{
   List<T> elements = new List<T>();
   new public void Clear() { elements.Clear(); }
   new public void Add(T element) { elements.Add(element); }
   new public int Count { get { return elements.Count; } }    
   new public IEnumerator<T> GetEnumerator()
   {
      foreach ( T c in elements )
         yield return c;
   }

   new public T this[int index]
   {
      get
      {
         foreach ( T c in elements ) {
            if ( (int)c.Id == index )
               return c;
         }
         return default(T);
      }
   }
}

```

And that's how I use it:

```
class ButtonWithId: Button, IHasId
{
   public int Id { get; private set; }
   public ButtonWithId(int id) { this.Id = id; }
}

IndexerList<ButtonWithId> buttons = new IndexerList<ButtonWithId>();
void AddButtons()
{
   for ( int i = 10; i <= 20; i++ ) {
      var button = new ButtonWithId(i);
      buttons.Add(button);
      button.Click += (sender, e) => 
         MessageBox.Show(String.Format("You clicked button number {0}", ( (ButtonWithId)sender ).Id));
   }
}

```

I don't have to make a `for` loop over my collection in order to find the index. My Button has an ID and this is also used as index in `IndexerList<T>`, so **you avoid any redundant ID's or indexes** - that's what I like! The index/Id can be an arbitrary number.
I have a small db data layer that has a `command` class in which you set the SQL command text, the command type, and return a IEnumerable of 'command parameters'.

Basically the idea is to have typed CLR commands instead of manually filling `SqlCommand` properties and parameters all the time. 

So there is a function that looks like this:

```
IEnumerable<DbParameter> GetParameters()
{
    // here i do something like

    yield return new DbParameter { name = "@Age", value = this.Age };

    yield return new DbParameter { name = "@Name", value = this.Name };
}

```

The class that inherits this `command` class has the properties `Age` and `Name`. 

Then you can new up a `command` object filled its properties and pass it to a `db` interface which actually does the command call. 

All in all makes it really easy to work with SQL commands and keep them typed.