7
在下面的C#代碼中,我發現_()
的用法很奇怪。誰能解釋這是什麼意思?語法的含義:return _(); IEnumerable <TSource> _()
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
return _(); IEnumerable<TSource> _()
{
var knownKeys = new HashSet<TKey>(comparer);
foreach (var element in source)
{
if (knownKeys.Add(keySelector(element)))
yield return element;
}
}
}
我*真的*希望這種模式不趕上。這只是醜陋的。 –
我從不喜歡本地函數如何缺少任何類型的標識關鍵字 - 例如,它們很容易被混淆爲本地代理或對象初始化列表。一個簡單的'func'關鍵字或強制lambda語法'=>'會有幫助)。 – Dai
無論如何,本地功能似乎沒有必要。 –