C#: Dynamic Iterators with Yield Return
2007-03-22
2007-03-22
| Table of Contents: |
| Rate This Article: | Add This Article To: |
C#: Dynamic Iterators with Yield Return - ' Listing 2 '
( Page 3 of 3 )
( Page 3 of 3 )
Listing 2: The ScrabbleWord, Dictionary, and yield return (in bold) code.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace UsingYield
{
class Program
{
static void Main(string[] args)
{
Dictionary d = Dictionary.Create();
//foreach(ScrabbleWord word in d)
foreach(ScrabbleWord word in
d.Get(@"^A\w+E$"))
Console.WriteLine(word);
Console.WriteLine("enter");
Console.ReadLine();
}
public class Dictionary : List<ScrabbleWord>
{
public void Add(string word,
string description, int points)
{
this.Add(new ScrabbleWord(word,
description, points));
}
public static Dictionary Create()
{
Dictionary dic = new Dictionary();
dic.Add("AWE", "three letter word", 6);
dic.Add("AWL", "three letter word", 5);
dic.Add("AWN", "three letter word", 5);
dic.Add("AXE", "three letter word", 2);
dic.Add("AYE", "three letter word", 6);
dic.Add("AYS", "three letter word", 5);
dic.Add("AZO", "three letter word", 12);
dic.Add("BAA", "three letter word", 4);
dic.Add("BAD", "three letter word", 4);
dic.Add("BAG", "three letter word", 4);
dic.Add("BAH", "three letter word", 4);
dic.Add("BAL", "three letter word", 4);
dic.Add("BAM", "three letter word", 4);
dic.Add("BAN", "three letter word", 4);
dic.Add("BAP", "three letter word", 4);
dic.Add("BAR", "three letter word", 4);
dic.Add("BAS", "three letter word", 4);
dic.Add("BAT", "three letter word", 4);
dic.Add("BAY", "three letter word", 7);
dic.Add("BED", "three letter word", 4);
dic.Add("BEE", "three letter word", 5);
dic.Add("BEG", "three letter word", 4);
dic.Add("BEL", "three letter word", 4);
dic.Add("BEN", "three letter word", 4);
dic.Add("BET", "three letter word", 4);
return dic;
}
public IEnumerable<ScrabbleWord> Get(
string regex)
{
foreach(ScrabbleWord o in this)
if(Regex.IsMatch(o.Word, regex))
yield return o;
}
}
public class ScrabbleWord
{
private string word;
public string Word
{
get
{
return word;
}
set
{
word = value;
}
}
private string description;
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
private int points;
public int Points
{
get
{
return points;
}
set
{
points = value;
}
}
/// <summary>
/// Initializes a new instance of
/// the ScrabbleWord class.
/// </summary>
/// <param name="word"></param>
/// <param name="description"></param>
/// <param name="points"></param>
public ScrabbleWord(string word,
string description, int points)
{
this.word = word;
this.description = description;
this.points = points;
}
public override string ToString()
{
return string.Format(
"Word: {0}, Description: {1}, Points: {2}",
word, description, points);
}
}
}
}
Biography
Paul Kimmel is a software architect for TriState Hospital Supply, columnist, book author, Microsoft MVP, and .NET User Group president. You may contact him at pkimmel@softconcepts.com if you have technology questions or are interested in joining or starting a users group.
![]() |
|


