如何将附加参数传递给谓词函数?

发布于 2024-09-15 02:23:56 字数 301 浏览 4 评论 0原文

我可以将附加参数传递给谓词函数吗?

我实际上在排序过程中需要它。

public void Sort(
    Comparison<T> comparison
)

我想以这种形式使用 Comparison 谓词:

public delegate int Comparison<T>(
    T x,
    T y,
    object extraParameter
)

这可能吗?

Can I pass additional parameters to a predicate function?

I need it in a sorting process actually.

public void Sort(
    Comparison<T> comparison
)

where I would like to use the Comparison predicate in this form:

public delegate int Comparison<T>(
    T x,
    T y,
    object extraParameter
)

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转身泪倾城 2024-09-22 02:23:56

不,但你可以这样做:

public Comparison<T> MakeComparison<T>(object extraParameter)
{
    return
        delegate(T x, T y) 
        {
            // do comparison with x, y and extraParameter
        }
}

No, but you can do this:

public Comparison<T> MakeComparison<T>(object extraParameter)
{
    return
        delegate(T x, T y) 
        {
            // do comparison with x, y and extraParameter
        }
}
林空鹿饮溪 2024-09-22 02:23:56

声明谓词时简单捕获所需的变量。例如:

int i = 0, j = 10;

array.Sort(x => x > i && x < j ? 1 : -1);

Simple capture the variables you need when you declare the predicate. Eg:

int i = 0, j = 10;

array.Sort(x => x > i && x < j ? 1 : -1);
深海夜未眠 2024-09-22 02:23:56

参加聚会有点晚了,所以我回答这个问题,以防有人通过谷歌搜索发现这里(就像我所做的那样)。

您不能传递多个参数,但您可以采取一些技巧来避开该限制:

使谓词需要 Tuple (或值元组),这样您可以通过创建元组并将其传递给谓词来传递更多值。据我所知,最大的元组最多有 3 个参数,但如果您需要传递更多参数,您可以轻松创建自己的元组。

Edit1:我建议这样做而不是上面的答案的原因是,有时您没有直接声明谓词,而是使用返回 bool 作为谓词的方法。

例如:

public class Tuple<T1,T2,T3,T4>
{
    public T1 Item1;
    public T2 Item2;
    public T3 Item3;
    public T4 Item4;
    
    public Tuple(T1 item1, T2 item2, T3 item3, T4 item4)
    {
       Item1 = item1;
       Item2 = item2;
       Item3 = item3;
       Item4 = item4;
    }
    
}

public struct ValueTuple<T1,T2,T3,T4>
{
    public T1 Item1;
    public T2 Item2;
    public T3 Item3;
    public T4 Item4;
    
    public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4)
    {
       Item1 = item1;
       Item2 = item2;
       Item3 = item3;
       Item4 = item4;
    }
}

A bit late to the party so i am answering this in case anyone comes across here by a google search (as i did).

You cannot pass more than parameter but you can do a trick to dodge that limitation:

Make it so the predicate requires a Tuple (or value tuple), that way you can pass more values by creating the tuple and passing it to the predicate. As far as i know the biggest tuple has a maximum of 3 parameters, but you can easily create your own tuples if you need to pass more parameters.

Edit1: The reason i suggested this instead of the above answers is that sometimes you are not declaring the predicate directly and instead using a method that returns bool as the predicate.

E.g:

public class Tuple<T1,T2,T3,T4>
{
    public T1 Item1;
    public T2 Item2;
    public T3 Item3;
    public T4 Item4;
    
    public Tuple(T1 item1, T2 item2, T3 item3, T4 item4)
    {
       Item1 = item1;
       Item2 = item2;
       Item3 = item3;
       Item4 = item4;
    }
    
}

public struct ValueTuple<T1,T2,T3,T4>
{
    public T1 Item1;
    public T2 Item2;
    public T3 Item3;
    public T4 Item4;
    
    public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4)
    {
       Item1 = item1;
       Item2 = item2;
       Item3 = item3;
       Item4 = item4;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文