मैं इस के लिए एक प्रकार निष्कर्ष सहायक बनाया। मैं वास्तव में lambdas के हस्ताक्षर को लिखकर पसंद नहीं है मैं उन्हें अस्थायी चर में संग्रहीत करने के लिए तो मैं लिखने
var fn = Func.F((string x) => MessageBox.Show(x));
या
var fn = Func.F((double x, double y) => x + y);
तुम अब भी पैरामीटर signiture में डालने के लिए करना चाहते हैं लेकिन आप रिटर्न प्रकार के साथ टाइप अनुमान अनुमान दें।
कार्यान्वयन
using System;
namespace System
{
/// <summary>
/// Make type inference in C# work harder for you. Normally when
/// you want to declare an inline function you have to type
///
/// Func<double, double, double> fn = (a,b)=>a+b
///
/// which sux! With the below methods we can write
///
/// var fn = Func.F((double a, double b)=>a+b);
///
/// which is a little better. Not as good as F# type
/// inference as you still have to declare the args
/// of the function but not the return value which
/// is sometimes not obvious straight up. Ideally
/// C# would provide us with a keyword fun used like
///
/// fun fn = (double a, double b)=>a+b;
///
/// but till then this snippet will make it easier
///
/// </summary>
public static class Func
{
public static Func<A> F<A>(Func<A> f)
{
return f;
}
public static Func<A,B> F<A, B>(Func<A, B> f)
{
return f;
}
public static Func<A,B,C> F<A, B,C>(Func<A, B,C> f)
{
return f;
}
public static Func<A,B,C,D> F<A,B,C,D>(Func<A,B,C,D> f)
{
return f;
}
public static Func<A,B,C,D,E> F<A,B,C,D,E>(Func<A,B,C,D,E> f)
{
return f;
}
public static Action A(Action f)
{
return f;
}
public static Action<_A> A<_A>(Action<_A> f)
{
return f;
}
public static Action<_A,B> A<_A, B>(Action<_A, B> f)
{
return f;
}
public static Action<_A,B,C> A<_A, B,C>(Action<_A, B,C> f)
{
return f;
}
public static Action<_A,B,C,D> A<_A,B,C,D>(Action<_A,B,C,D> f)
{
return f;
}
public static Action<_A,B,C,D,E> A<_A,B,C,D,E>(Action<_A,B,C,D,E> f)
{
return f;
}
}
}
या 'कार्रवाई है' या 'समारोह <स्ट्रिंग, DialogResult>' –
सामान्य तौर पर, मैं सुझाव है कि आप एक उन्नत सुविधा सामान्य कोड में परहेज किया जाता के रूप में dynamic' इलाज '। – Brian