首页 文章

'System.Collections.Generic.List<float>'不包含'Sum'的定义

提问于
浏览
14

我试图用内置 Sum() 函数来汇总浮点数列表但我一直收到此错误:

错误CS1061:'System.Collections.Generic.List'不包含'Sum'的定义,并且没有可以找到接受类型'System.Collections.Generic.List'的第一个参数的扩展方法'Sum'(是你吗?缺少using指令或汇编引用?)(CS1061)

我有

using System.Collections;
using System.Collections.Generic;

在文件的开头:

代码:

List<float> x = new List<float>();
x.add(5.0f);
//..
float f = x.Sum();

1 回答

  • 30

    您需要添加到 using 指令:

    using System.Linq;
    

    此外,您的代码在语法上是错误的 . 这是工作版本:

    var x = new List<float>();
    x.Add(5.0f);
    var f = x.Sum();
    

相关问题