[.NET 2.0] Testing Generics, Collections and Yield Return
I've not looked too much at the yield return statement in c# 2.0 yet, but if you learn how to use it I'm sure it will become something you use "every day" when programming. Whenever you need to do some conditional iterations on a collection, array or list, yield may be a nice solution to go for. A few, silly and not perfect, snippets which helped me understand how to use yield return:
using System;
using System.Collections;
using System.Collections.Generic;
public class Program
{
static void Main()
{
// Build up a few arrays and a list to work with
string[] names = { "Johan", "Per", "Thomas", "Lina", "Juan", "Jan-Erik", "Mats" };
int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
Console.WriteLine("Get the names which contains an 'a':");
// Iterate on all names which contains letter 'a'
foreach (string name in EnumerableUtils<string>.GetNamesContainingLetter(names, 'a'))
{
Console.Write("{0} ", name);
}
Console.WriteLine();
Console.WriteLine();
//iterate through first half of this collection
Console.WriteLine("First half of the name-array:");
foreach (string name in EnumerableUtils<string>.GetFirstHalfFromList(names))
{
Console.Write("{0} ", name);
}
Console.WriteLine();
Console.WriteLine();
//iterate through first half of another collection
Console.WriteLine("First half of the value-array:");
foreach (int value in EnumerableUtils<int>.GetFirstHalfFromList(values))
{
Console.Write("{0} ", value);
}
Console.WriteLine();
Console.WriteLine();
//iterate through first half of a List
Console.WriteLine("First half of the value-list:");
foreach (int value in EnumerableUtils<int>.GetFirstHalfFromList(list))
{
Console.Write("{0} ", value);
}
Console.WriteLine();
}
}
public class EnumerableUtils<T>
{
public static IEnumerable GetNamesContainingLetter(string[] names, char letter)
{
foreach (string name in names)
{
if (name.ToLower().Contains(letter.ToString().ToLower()))
yield return name;
}
}
public static IEnumerable<T> GetFirstHalfFromList(List<T> arr)
{
for (int i = 0; i < arr.Count / 2; i++)
yield return arr[i];
}
public static IEnumerable<T> GetFirstHalfFromList(T[] arr)
{
for (int i = 0; i < arr.Length / 2; i++)
yield return arr[i];
}
}
using System.Collections;
using System.Collections.Generic;
public class Program
{
static void Main()
{
// Build up a few arrays and a list to work with
string[] names = { "Johan", "Per", "Thomas", "Lina", "Juan", "Jan-Erik", "Mats" };
int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
Console.WriteLine("Get the names which contains an 'a':");
// Iterate on all names which contains letter 'a'
foreach (string name in EnumerableUtils<string>.GetNamesContainingLetter(names, 'a'))
{
Console.Write("{0} ", name);
}
Console.WriteLine();
Console.WriteLine();
//iterate through first half of this collection
Console.WriteLine("First half of the name-array:");
foreach (string name in EnumerableUtils<string>.GetFirstHalfFromList(names))
{
Console.Write("{0} ", name);
}
Console.WriteLine();
Console.WriteLine();
//iterate through first half of another collection
Console.WriteLine("First half of the value-array:");
foreach (int value in EnumerableUtils<int>.GetFirstHalfFromList(values))
{
Console.Write("{0} ", value);
}
Console.WriteLine();
Console.WriteLine();
//iterate through first half of a List
Console.WriteLine("First half of the value-list:");
foreach (int value in EnumerableUtils<int>.GetFirstHalfFromList(list))
{
Console.Write("{0} ", value);
}
Console.WriteLine();
}
}
public class EnumerableUtils<T>
{
public static IEnumerable GetNamesContainingLetter(string[] names, char letter)
{
foreach (string name in names)
{
if (name.ToLower().Contains(letter.ToString().ToLower()))
yield return name;
}
}
public static IEnumerable<T> GetFirstHalfFromList(List<T> arr)
{
for (int i = 0; i < arr.Count / 2; i++)
yield return arr[i];
}
public static IEnumerable<T> GetFirstHalfFromList(T[] arr)
{
for (int i = 0; i < arr.Length / 2; i++)
yield return arr[i];
}
}