C# NewLanguageFeatures lambda表达式使用n重要性
我正在学习lambda表达式和扩展方法的概念:-C# NewLanguageFeatures lambda表达式使用n重要性,c#,lambda,C#,Lambda,我正在学习lambda表达式和扩展方法的概念:- namespace NewLanguageFeatures { public delegate bool KeyValuePair<K, V>(K key, V value); /// <summary> /// Task 13 – Calling a Complex Extension Method using Lambda Expressions [ops] /// </summary> public
namespace NewLanguageFeatures
{
public delegate bool KeyValuePair<K, V>(K key, V value);
/// <summary>
/// Task 13 – Calling a Complex Extension Method using Lambda Expressions [ops]
/// </summary>
public static class LambdaExtensions
{
//This extension method takes in a dictionary and a delegate
public static List<K> FilterBy<K, V>(
this Dictionary<K, V> items,
KeyValueFilter<K, V> filter)
{
var result = new List<K>();
foreach (KeyValuePair<K, V> element in items)
{
if (filter(element.Key, element.Value))
result.Add(element.Key);
}
return result;
}
}
}
名称空间NewLanguageFeatures
{
公共委托bool KeyValuePair(K键,V值);
///
///任务13–使用Lambda表达式调用复杂扩展方法[ops]
///
公共静态类LambdaExtensions
{
//此扩展方法接受字典和委托
公共静态列表过滤器(
这本词典收录了下列项目:,
KeyValueFilter(过滤器)
{
var result=新列表();
foreach(项中的KeyValuePair元素)
{
if(过滤器(element.Key,element.Value))
结果.添加(元素.键);
}
返回结果;
}
}
}
它给出了以下错误:
Error 1 Cannot convert type 'System.Collections.Generic.KeyValuePair<K,V>' to 'NewLanguageFeatures.KeyValuePair<K,V>' and
Error 2 'NewLanguageFeatures.KeyValuePair<K,V>' does not contain a definition for 'Key' and no extension method 'Key' accepting a first argument of type 'NewLanguageFeatures.KeyValuePair<K,V>' could be found (are you missing a using directive or an assembly reference?)
错误1无法将类型“System.Collections.Generic.KeyValuePair”转换为“NewLanguageFeatures.KeyValuePair”,并且
错误2“NewLanguageFeatures.KeyValuePair”不包含“Key”的定义,并且找不到接受“NewLanguageFeatures.KeyValuePair”类型的第一个参数的扩展方法“Key”(是否缺少using指令或程序集引用?)
有什么好主意吗?这只是你代码中的一个输入错误:
public delegate bool KeyValuePair<K, V>(K key, V value);
您的项目中是否在NewLanguageFeatures命名空间中定义了KeyValuePair?我完全不知道问题标题的含义。。。
public delegate bool KeyValueFilter<K, V>(K key, V value);
var filtered = existing.Where(pair => pair.Key == pair.Value);
// ^^^ just a random filter to show usage; nothing significant here