読者です 読者をやめる 読者になる 読者になる

Extension Method - Int

C#
using System;
using System.Linq;
using System.Collections.Generic;

namespace Shand.Extension
{
    public static partial class Extensions
    {
        public static IEnumerable<int> To(this int from, int to)
        {
            return Enumerable.Range(from, to);
        }

        public static void Times(this int count, Action<int> action)
        {
            for (int i = 1; i <= count; i++)
            {
                action(i);
            }
        }

        public static void Times(this int count, Action action)
        {
            for (int i = 0; i < count; i++)
            {
                action();
            }
        }
    }
}


Range です

foreach (int i in 1.To(5))
{
    Console.WriteLine(i);
}


Ruby っぽい?

10.Times(x =>
{
    Console.WriteLine(x);
});

カウントがいらない場合はこっち

10.Times(() =>
{
    Console.WriteLine("Hello");
});