Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.Collections.Generic.List<float>' does not contain a definition for 'Sum'

Tags:

c#

list

sum

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();
like image 662
Patryk Avatar asked Jan 12 '13 16:01

Patryk


1 Answers

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();
like image 80
Mir Avatar answered Oct 03 '22 01:10

Mir