Skip to content

Instantly share code, notes, and snippets.

@leviwilson
Created November 7, 2011 16:08
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 leviwilson/1345379 to your computer and use it in GitHub Desktop.
Save leviwilson/1345379 to your computer and use it in GitHub Desktop.
Example Extension Methods
public static class EnumerableExtensions
{
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
action(item);
}
}
public static class HashCode
{
public static int ComputeHashCode(params object[] logicalKeys)
{
var hash = 13;
foreach (var logicalKey in logicalKeys)
{
hash = hash * 7 + (logicalKey == null ? 0 : logicalKey.GetHashCode());
}
return hash;
}
}
public static class IntegerExtensions
{
public static void Times(this int n, Action<int> action)
{
Enumerable.Range(0, n).ForEach(action);
}
}
@leviwilson
Copy link
Author

public void Foo() {
  10.Times(x => DoSomethingWith(x));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment