Saturday, December 4, 2010

Extension Methods

Where records is an IEnumerable of CustomRecord

List<int> numbers = records.GetNumbers();

In a public static class:

public static class DataExtension
{
    public static List<int> GetNumbers(this IEnumerable<CustomRecord> records)
    {
        var numbers = new List<int>();

        foreach (var record in records) {
            numbers.Add(record.Number);
        }

        return numbers;
    }
}

No comments: