Skip to content

Instantly share code, notes, and snippets.

@Swimburger
Created September 2, 2014 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swimburger/da8e1c72c9a8b546138d to your computer and use it in GitHub Desktop.
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.
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