将Ruby的时间转换为C#

时间:2012-06-12 10:01:31

标签: c# ruby function

我正在尝试将Ruby的time转换为C#,但我现在被卡住了。

这是我的尝试:

public static class Extensions
{
    public static void Times(this Int32 times, WhatGoesHere?)
    {
        for (int i = 0; i < times; i++)
            ???
    }
}

我是C#的新手,也许这个应该很简单,我知道我想使用Extensionmethods。但由于C#中的函数不是“头等”,我现在卡住了。

那么,我应该使用什么参数类型的 WhatGoesHere?

1 个答案:

答案 0 :(得分:5)

您可以使用Action类型:

public static class Extensions
{
    public static void Times(this Int32 times, Action<Int32> action)
    {
        for (int i = 0; i < times; i++)
            action(i);
    }
}

class Program
{
    delegate void Del();

    static void Main(string[] args)
    {
        5.Times(Console.WriteLine);
        // or
        5.Times(i => Console.WriteLine(i));
    }
}

还要看一下here来了解代表。