c# - List.Foreach() 提示无法从 'void' 转换为 'object'

标签 c# .net-core

当我尝试运行以下代码时,它在最后一行告诉我“无法从‘void’转换为‘object’,有人知道代码有什么问题吗?

public static void Main(string[] args)
{
    string s = "asfdsanfdjsajfois";
    Dictionary<char, int> dict = new Dictionary<char, int>();
    s.ToCharArray().ToList().ForEach(a => {
        dict[a] = (dict.ContainsKey(a)) ? dict[a]+1 : 1;
    });
    pln(dict.Keys.ToList().ForEach(a => Console.WriteLine(dict[a])));
}

最佳答案

问题很明显,无需提供更多信息:

dict.Keys.ToList().ForEach(a => Console.WriteLine(dict[a])) 是一个不返回任何内容的语句 void ,然后将其传递给 pln 函数,该函数接受一个类型为 object 的参数。您需要向它传递一个该类型的对象,以使编译器满意并使代码有意义。

关于c# - List.Foreach() 提示无法从 'void' 转换为 'object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821448/

相关文章:

c# - 对单个 DataGridView 列的验证

c# - Outlook 插件项目保存

c# - 以非 ASCII 语言编码

.Net Core Nuget 包版本不匹配 : Two packages have references to different versions of the same dll

c# - SpreadJS 在基于 Webpack 的 Angular 应用程序中不起作用

c# - .Net 5发布单个文件-生成exe和dll

c# - 将调试启动外部程序选项设置为 Visual Studio 中类库的可执行文件

c# - jQuery 浏览器差异

.net-core - 步长大于 1 的数组并行迭代

c# - Blazor 服务器构建错误 - 新的 Web 应用程序导致构建错误