14
votes

I am trying to Sum a list of floats with built in Sum() function but I keep getting this error :

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)

and I have

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

in the beginning of the file:

code :

List<float> x = new List<float>();
x.add(5.0f);
//..
float f = x.Sum();
1
Why am I getting - for this question ?Patryk

1 Answers

32
votes

You need to add to your using directives:

using System.Linq;

Besides, your code is syntactically wrong. Here's the working version:

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