c# - 如何在不使用 c# 初始化的情况下使用 func deligate?
问题描述
当我在控制台应用程序中运行此代码时,此代码返回“星号”,但我没有在“谓词函数”中初始化“索引”。这是如何运作的?有默认值吗?
Func<String, int, bool> predicate = (str, index) => str.Length == index;
String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);
foreach (String word in aWords)
Console.WriteLine(word);
解决方案
有两个重载:Enumerable.Where
- 一个采用常规谓词的形式
Func<TSource, Boolean>
和 - 另一个,它接受表单的谓词
Func<TSource, Int32, Boolean>
并将元素及其索引传递给谓词。
您的代码使用第二个重载。
推荐阅读
- java - 为什么我的布尔值被抢先返回?
- android - 广播接收器在android studio应用程序中一直不工作
- reactjs - 带有文本的 findByRole listitem 找不到带有文本的 `li` 元素
- javascript - 如果一个位置值不是 1,为什么数组中两个位置之间的比较是错误的?
- r - 如何在ggplot中“分组”一个图例
- python - 超过 Lambda 函数最大递归深度
- javascript - 类列表的本地存储
- java - git share 后 Eclipse maven EAR 项目不工作
- python - 如何使用python openpyxl从excel中按列名删除列
- node.js - Puppeteer 与 Express Router Node JS 的并行性。如何在保持并发的同时在路由之间传递页面