Skip to content

Instantly share code, notes, and snippets.

@rohinomiya
Created August 29, 2012 15:25
Show Gist options
  • Save rohinomiya/3514320 to your computer and use it in GitHub Desktop.
Save rohinomiya/3514320 to your computer and use it in GitHub Desktop.
ForEach()メソッドをシンプルに書けるようにする ref: http://qiita.com/items/99aa993cfe5def51bbe5
using System;
namespace Rohinomiya
{
/// <summary>
/// Arrayクラスを拡張する
/// </summary>
public static class ArrayExtension
{
/// <summary>
/// ForEachメソッド
/// </summary>
/// <param name="array">Arrayクラス</param>
/// <param name="action">実行させたいActionデリゲート</param>
public static void ForEach<T>(this T[] array, Action<T> action)
{
Array.ForEach<T>(array, obj => action(obj));
}
}
}
using System;
using Rohinomiya;
namespace SampleForEach
{
class Program
{
public static void Main(string[] args)
{
var dirs = new string[]{"aa","bb","cc"};
Array.ForEach<string>(dirs, s => Console.WriteLine(s) ); // a
dirs.ForEach(s => Console.WriteLine(s)); // b:こういう書き方したい
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment