我如何使用闭包在 C# 中重写此 JavaScript 例程?
How would I rewrite this JavaScript routine in C# using closure?
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));
您可以在 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 语法(是的,C# 自 2005 年以来就已经关闭)
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# 中基于 class 解决此问题的传统方法是:
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 );
}
}
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));
您可以在 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 语法(是的,C# 自 2005 年以来就已经关闭)
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# 中基于 class 解决此问题的传统方法是:
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 );
}
}