0

I am getting an error

Error CS1929 'uint[]' does not contain a definition for 'Average' and the best extension method overload 'Queryable.Average(IQueryable)' requires a receiver of type 'IQueryable' ArrayLengthTest D:\Visual Studio\ArrayLengthTest\ArrayLengthTest\Program.cs 17 Active

uint[] asd = new uint[5];
            for (uint i = 0; i < asd.Length; i++)
            {
                asd[i] = i;
            }
            uint arrayAverage = asd.Average();

Any ideas what does it mean? I know that I can easily use ints but I am asking for uints...

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
pesho
  • 11
  • 1
  • 2
    It means there's no such method as Average() on uint[]. – Steve Smith Feb 15 '17 at 16:56
  • You can have a [look at the docs](https://msdn.microsoft.com/en-us/library/bb358602(v=vs.110).aspx). `uint[]` doesn't have the `Average()` function. – Drew Kennedy Feb 15 '17 at 17:00
  • @DrewKennedy and why it doesn't have? I mean if you need it... ofc the result will be approximately since Average returns double type... but still? – pesho Feb 15 '17 at 17:04
  • @rene not a duplicate.. I know that i can use int to calculate the average, but my question is if i want to use uint for average... – pesho Feb 15 '17 at 17:07
  • Here's a question about why `int` is preferred over `uint` in the framework, I think that's as close to an answer as you can get. http://stackoverflow.com/questions/782629/why-does-net-use-int-instead-of-uint-in-certain-classes – juharr Feb 15 '17 at 17:18

1 Answers1

0

You can use

using System.Linq;

or see this post how-can-i-return-the-sum-average-of-an-int-array

UPDATE [using refl] you can try to implement your custom Avarage extension [this is a semple idea]

public static uint Average(this IEnumerable<uint> source)
    {
        if (source == null) throw new Exception(); 
        long sum = 0;
        long count = 0;
        checked
        {
            foreach (uint v in source)
            {
                sum += v;
                count++;
            }
        }
        if (count > 0) return(uint)((double)sum / count);
        throw new Exception();
    }
Community
  • 1
  • 1
andmattia
  • 357
  • 6
  • 22