Created
September 2, 2014 18:22
-
-
Save Swimburger/da8e1c72c9a8b546138d to your computer and use it in GitHub Desktop.
An example console application that shows you how to create an extension method on a collection that prints its content to the console in a generic way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var persons = new List<Person>{ | |
new Person{FirstName="Gilles",LastName="achternaam"}, | |
new Person{FirstName="Nog",LastName="Iemand anders"}, | |
new Person{FirstName="Delaatse",LastName="persoon"} | |
}; | |
persons.LogCollection(); | |
} | |
} | |
public class Person | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public override string ToString() | |
{ | |
return FirstName + ' ' + LastName; | |
} | |
} | |
public static class CollectionExtension | |
{ | |
public static void LogCollection<T>(this IEnumerable<T> collection) | |
{ | |
foreach(T item in collection) | |
{ | |
System.Diagnostics.Debug.WriteLine(item); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment