What will this code YIELD
Can you figure out what the following code will yield on the screen.
| using System.Collections.Generic; using System.Windows.Forms;
namespace YieldReturnSpike { public partial class Form1 : Form { public Form1() { InitializeComponent();
uxPeopleGridUsingYield.DataSource = new People().GetPeopleIWorkWith();
uxPeopleGridNotUsingYield.DataSource = new List<Person>(new People().GetPeopleIWorkWith()); } }
public class People { public IEnumerable<Person> GetPeopleIWorkWith() { yield return new Person("Mo Khan"); yield return new Person("Joel Briggs"); yield return new Person("Luu Duong"); yield return new Person("Matt Gardiner"); } }
public class Person { private readonly string name;
public Person(string name) { this.name = name; }
public string Name { get { return name; } } } } |
Here are the puzzling results:
As you can see when the data source is set to a list with deferred execution the list is not bound to the data grid. I really don't understand why this happens. In order to figure this one out I will have to gain more insight into how the databinding works under the hood. I'll post my findings once I get some answers.

1 comments:
The DataGridView does not support IEnumerable as a data source, so it treats the binding as if you are binding to an object that has no properties (of type IEnumerable person).
The List version enumerates the people you work with immediately in it's constructor, so you're binding just a bog standard list of Person.
Post a Comment