WriteToConsole
Write all elements in the Enumeration to the Console
Source
using System;
using System.Collections.Generic;
namespace ExtensionMethods {
public static class Extensions {
/// <summary>
/// Write all elements in the Enumeration to the Console
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
public static void WriteToConsole<T>(this IEnumerable<T> list) {
foreach (var obj in list) {
Console.WriteLine(obj);
}
}
/// <summary>
/// Write all elements in the Enumeration to the Console
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">the enumeration written to the list</param>
/// <param name="predicate">a transform function to apply to each element</param>
public static void WriteToConsole<T>(this IEnumerable<T> list, Func<T, object> transfer) {
foreach (var obj in list) {
Console.WriteLine(transfer(obj));
}
}
}
}
Example
// Uses class Employee with Name and Salary
var l = new List<Employee>()
{
new Employee("Fons", 2000),
new Employee("Jim", 3000),
new Employee("Ellen", 4000)
};
l.WriteToConsole();
l.WriteToConsole(emp => emp.Name);
Author: Fons Sonnemans
Submitted on: 10 dec. 2007
Language: C#
Type: System.Collections.Generic.IEnumerable<T>
Views: 5552