Wednesday, February 20, 2008

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.

3 comments:

Anonymous said...

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.

marko said...

In the event that the borrower is neglected to make a reimbursement of the cash obtained. This was maybe one of the primary reasons why loaning establishments did not waver to offer advances to individuals that had not as much as stellar FICO scores. www.usacheckcashingstore.com/san-diego

marko said...

When you took out your advances for instructive purposes, it was an extensive procedure loaded with printed material. At the time you most likely gave careful consideration to the points of interest of the understandings, for example, to what extent you had as an elegance period before reimbursement was to start, what kind of financing cost you would be charged and even how much the aggregate sum of advances would be as you proceeded through school. Payday Loans Corona