gpt4 book ai didi

c# - 'System.Collections.Generic.List' does not contain a definition for ' Sum'

转载 作者:可可西里 更新时间:2023-11-01 07:51:47 28 4
gpt4 key购买 nike

我正在尝试使用内置的 Sum() 函数对 float 列表求和,但我不断收到此错误:

Error CS1061: 'System.Collections.Generic.List' does not contain a definition for 'Sum' and no extension method 'Sum' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?) (CS1061)

我有

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

在文件的开头:

代码:

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

最佳答案

您需要添加到您的using 指令:

using System.Linq;

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

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

关于c# - 'System.Collections.Generic.List<float >' does not contain a definition for ' Sum',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14295301/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com