More Pertinent Usage

suggest change
public IEnumerable<User> SelectUsers()
{
    // Execute an SQL query on a database.
    using (IDataReader reader = this.Database.ExecuteReader(CommandType.Text, "SELECT Id, Name FROM Users"))
    {
        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string name = reader.GetString(1);
            yield return new User(id, name);
        }
    }
}

There are other ways of getting an IEnumerable<User> from an SQL database, of course – this just demonstrates that you can use yield to turn anything that has “sequence of elements” semantics into an IEnumerable<T> that someone can iterate over.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Yield Keyword:
* More Pertinent Usage

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
117 Yield Keyword
127 Caching
135 Pointers
147 C# Script