我如何使用闭包在C#中重写这个JavaScript例程?

时间:2015-01-15 19:23:49

标签: javascript c# closures

JavaScript代码:

function makeConverter(toUnit, factor, offset) {
    offset = offset || 0;

    var converter = function (input) {
        return ((offset + input) * factor).toFixed(2) + " " + toUnit;
    };

    return converter;
}

var milesToKm = makeConverter('km', 1.60936);
var poundsToKg = makeConverter('kg', 0.45460);
var farenheitToCelsius = makeConverter('degrees C', 0.5556, -32);

log(milesToKm(10));
log(poundsToKg(2.5));
log(farenheitToCelsius(98));

1 个答案:

答案 0 :(得分:0)

您可以在C#中执行相同的操作:

public static Func<double, string> makeConverter(string toUnit, double factor, double offset = 0.0)
{
    return (input) => string.Format("{0:0.##} {1}", (offset + input) * factor, toUnit);
}

public static void Main()
{
    var milesToKm = makeConverter("km", 1.60936);
    var poundsToKg = makeConverter("kg", 0.45460);
    var farenheitToCelsius = makeConverter("degrees C", 0.5556, -32);

    Console.WriteLine("{0}", milesToKm(10));
    Console.WriteLine("{0}", poundsToKg(2.5));
    Console.WriteLine("{0}", farenheitToCelsius(98));
}

您可以进一步将其缩减为单一方法:

public static void Main()
{
    Func<string, double, double, Func<double, string>> makeConverter =
        (toUnit, factor, offset) => (input) => string.Format("{0:0.##} {1}", (offset + input) * factor, toUnit);

    var milesToKm = makeConverter("km", 1.60936, 0.0);
    var poundsToKg = makeConverter("kg", 0.45460, 0.0);
    var farenheitToCelsius = makeConverter("degrees C", 0.5556, -32);

    Console.WriteLine("{0}", milesToKm(10));
    Console.WriteLine("{0}", poundsToKg(2.5));
    Console.WriteLine("{0}", farenheitToCelsius(98));
}

您甚至可以使用旧式可以使用C#2语法(是的,自2005年以来C#已经关闭)

public delegate string del_t(double input);

public static del_t makeConverter(string toUnit, double factor, double offset = 0.0)
{
    return delegate(double input) 
    {
        return string.Format("{0:0.##} {1}", (offset + input) * factor, toUnit );
    };
}

public static void Main()
{
    var milesToKm = makeConverter("km", 1.60936);
    var poundsToKg = makeConverter("kg", 0.45460);
    var farenheitToCelsius = makeConverter("degrees C", 0.5556, -32);

    Console.WriteLine("{0}", milesToKm(10) );
    Console.WriteLine("{0}", poundsToKg(2.5) );
    Console.WriteLine("{0}", farenheitToCelsius(98));
}

为了比较,如果你没有使用闭包,那么在C#中解决这个问题的传统的基于类的方法是:

public static void Main()
{
    var milesToKm = new Converter("km", 1.60936);
    var poundsToKg = new Converter("kg", 0.45460);
    var farenheitToCelsius = new Converter("degrees C", 0.5556, -32);

    Console.WriteLine("{0}", milesToKm.Convert(10) );
    Console.WriteLine("{0}", poundsToKg.Convert(2.5) );
    Console.WriteLine("{0}", farenheitToCelsius.Convert(98) );
}

class Converter
{
    string m_toUnit;
    double m_factor;
    double m_offset;

    public Converter(string toUnit, double factor, double offset = 0)
    {
        m_toUnit = toUnit;
        m_factor = factor;
        m_offset = offset;
    }

    public string Convert(double input)
    {
        return string.Format("{0:0.##} {1}", (m_offset + input) * m_factor, m_toUnit );
    }
}