私は(私はそれらの多くより多くを持っている)には、次の2つのような、非常に似ていますが、単一の行を探し機能の配列を有する:は、パラメータの関数として二つのパラメータでデリゲートを渡す
private static int HowManyHoursInTheFirstYear(IList<T> samples)
{
DateTime firstDate = samples[0].Date;
int count = 0;
while (count < samples.Count &&
samples[count].Date.Year == firstDate.Year)
{
count++;
}
return count;
}
private static int HowManyDaysInTheFirstMonth(IList<T> samples)
{
DateTime firstDate = samples[0].Date;
int count = 0;
while (count < samples.Count &&
samples[count].Date.Month == firstDate.Month) // <--- only change!
count++;
}
return count;
}
を
0:HowManyDaysInTheFirstPeriod(
samples,
delegate(DateTime d1, DateTime d2) { return d1.Month == d2.Month; });
が、それによって次のようにデリゲートを宣言:私はいくつかのエレガントな方法でコードでこの繰り返しを削除するために、デリゲートを使用して考えていた、それは私のようなものを呼び出すことが許されているだろう
とHowManyDaysInTheFirstPeriodは、以下のようなものにwhould:
残念ながらprivate static int HowManySamplesInFirstPeriod
IList<T> samples,
DateComparer comparer)
{
DateTime firstDate = samples[0].Date;
int count = 0;
while (count < samples.Count && comparer())
{
count++;
}
}
、コンパイラは、比較演算は2つのパラメータを必要と文句を言い。
私は比較的新しいC#であり、ここでロードブロックを打ちました。 これはどのように解決しますか?
ここで、C#を初めて使っている人は、これは非常に分かりやすい代理人の使い方です。反復を見つけ、それを代理人に分解するためのあなたへの誇り。 –