Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将带有两个参数的委托作为参数函数传递_C#_Delegates - Fatal编程技术网

C# 将带有两个参数的委托作为参数函数传递

C# 将带有两个参数的委托作为参数函数传递,c#,delegates,C#,Delegates,我有一系列看起来非常相似的函数,但只针对一行,如以下两个(但我有更多): 从而声明如下所示的委托: delegate bool DateComparer(DateTime first, DateTime second); private static int HowManySamplesInFirstPeriod IList<T> samples, DateComparer comparer) { DateTime firstDate = samples[0

我有一系列看起来非常相似的函数,但只针对一行,如以下两个(但我有更多):

从而声明如下所示的委托:

delegate bool DateComparer(DateTime first, DateTime second);
private static int HowManySamplesInFirstPeriod
    IList<T> samples,
    DateComparer comparer)
{
    DateTime firstDate = samples[0].Date;
    int count = 0;

    while (count < samples.Count && comparer())
    {
        count++;
    }
}
在第一个时期有多少天会是这样的:

delegate bool DateComparer(DateTime first, DateTime second);
private static int HowManySamplesInFirstPeriod
    IList<T> samples,
    DateComparer comparer)
{
    DateTime firstDate = samples[0].Date;
    int count = 0;

    while (count < samples.Count && comparer())
    {
        count++;
    }
}
private static int HowManySamplesInFirstPeriod
IList样本,
日期比较器(数据比较器)
{
DateTime firstDate=样本[0]。日期;
整数计数=0;
while(count
不幸的是,编译器抱怨比较器需要两个参数

我对C#比较陌生,在这里遇到了障碍。
您将如何解决此问题?

您需要向比较器传递所讨论的两个日期。这可能很简单:

private static int HowManySamplesInFirstPeriod
    IList<T> samples,
    DateComparer comparer)
{
    DateTime firstDate = samples[0].Date;
    int count = 0;

    while (count < samples.Count 
           && comparer(samples[count].Date, firstDate))
    {
        count++;
    }
}
private static int HowManySamplesInFirstPeriod
IList样本,
日期比较器(数据比较器)
{
DateTime firstDate=样本[0]。日期;
整数计数=0;
而(计数<样本数
&&比较器(样本[计数].Date,firstDate))
{
计数++;
}
}

或者您可能希望以另一种方式传递它们(即
firstDate
然后
samples[count].Date
)。

您需要传递与代表进行比较的日期。因此:

comparer(samples[count].Date, firstDate)

你快到了!
comparer
委托参数与任何其他函数一样:您仍然需要传递适当的参数来调用它。在您的情况下,这意味着以下更改:

while (count < samples.Count && comparer(samples[count].Date, firstDate))
{
    count++;
}
while(count

(另外,请注意,
samples
应该是
samples.Count
,正如我上面所写的那样。)

您可以做得简单一点。只需为函数提供一个委托,该委托从DateTime中提取应进行比较的内容:

private static int HowManySamplesInFirstPeriod<T>
    IList<T> samples,
    Func<DateTime, int> f // a function which takes a DateTime, and returns some number
{
    DateTime firstDate = samples[0].Date;
    int count = 0;

    while (count < samples && f(samples[count].Date) == f(firstDate))
    {
        count++;
    }
}
(dt)=>dt.year
语法对您来说可能是新的,但它是一种更简洁的编写“匿名委托的方式,它接受某种泛型类型的对象dt,并返回dt.year”。 您可以编写一个老式的委托,但这样更好。:)

不过,我们可以通过添加另一个泛型类型参数使其更为通用:

private static int HowManySamplesInFirstPeriod<T, U>
    IList<T> samples,
    Func<DateTime, U> f // Let's generalize it a bit, since the function may return something other than int (some of the DateTime members return doubles, as I recall)
private static int HowManySamplesInFirstPeriod
IList样本,
Func f//让我们对其进行一点概括,因为该函数可能返回int以外的内容(我记得,有些DateTime成员返回双倍)
但与往常一样,LINQ提供了一个更好的选择:

private static int HowManySamplesInFirstPeriod<T>
    IList<T> samples,
    Func<DateTime, int> f)
{
  var firstVal = f(samples.First().Date);
  return samples.Count(dt => f(dt.Date) = firstVal)
}
private static int HowManySamplesInFirstPeriod
IList样本,
函数(f)
{
var firstVal=f(samples.First().Date);
返回samples.Count(dt=>f(dt.Date)=firstVal)
}

我认为jalf的答案需要稍加修改,以适应最初的用法:

private static int HowManyHoursInTheFirstYear(IList<DateTime> samples, Func<DateTime, DateTime, bool> comparer) 
{ 
    DateTime firstDate = samples[0].Date; 
    int count = 0;
    while (count < samples.Count && comparer(samples[count], firstDate) ) {
        count++;
    } 
    return count; 
}

顺便说一句,对于那些说自己对C#相当陌生的人来说,这是一个非常明智的委托用法。感谢您发现了重复并将其分解为一个代表。
private static int HowManyHoursInTheFirstYear(IList<DateTime> samples, Func<DateTime, DateTime, bool> comparer) 
{ 
    DateTime firstDate = samples[0].Date; 
    int count = 0;
    while (count < samples.Count && comparer(samples[count], firstDate) ) {
        count++;
    } 
    return count; 
}
HowManyDaysInTheFirstPeriod(samples, (d1, d2) = > { return d1.Month == d2.Month; });
HowManyDaysInTheFirstPeriod(samples, (d1, d2) = > { return d1.Year == d2.Year; });